From e9a4a928f05084d311b9d9e2c31f4f1d9fa1dcb6 Mon Sep 17 00:00:00 2001 From: milesfrain Date: Fri, 1 Jan 2021 22:39:34 -0800 Subject: [PATCH] Add more examples for anonymous argument error --- errors/IncorrectAnonymousArgument.md | 41 ++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/errors/IncorrectAnonymousArgument.md b/errors/IncorrectAnonymousArgument.md index e377eae3..7aadee61 100644 --- a/errors/IncorrectAnonymousArgument.md +++ b/errors/IncorrectAnonymousArgument.md @@ -5,31 +5,54 @@ ```purescript module Example where -add = (_ + _) +sub3 = _ - 3 -mapArray = map _ [1, 2, 3] +square = (_ * _) +result1 = map (_ * _) [1, 2, 3] + +result2 = map (div _ 2) [1, 2, 3] + +result3 = map (1 + _ / 2) [1, 2, 3] ``` ## Cause -In an [operator section](https://github.com/purescript/documentation/blob/fc4a9db4b128aa3331e5f990cb1860e59077af31/language/Syntax.md#operator-sections), like `(_ + 1)`, an anonymous argument can be used only once. - +An [operator section](https://github.com/purescript/documentation/blob/master/language/Syntax.md#operator-sections), like `(_ + 1)`, has the following requirements: +- The expression must be bracketed (surrounded by parens). +- An anonymous argument can be used only once. +- The anonymous argument must neighbor a [binary operator](https://github.com/purescript/documentation/blob/master/language/Syntax.md#binary-operators). +- The anonymous argument must be the _sole expression_ for one of the binary operator arguments. ## Fix -In the case of multiple arguments, give them names: +Surround with parens: ```purescript -add a b = (a + b) +sub3 = (_ - 3) ``` -or in the case of a normal function: Write the function [as an operator](https://github.com/purescript/documentation/blob/fc4a9db4b128aa3331e5f990cb1860e59077af31/language/Syntax.md#functions-as-operators) using backticks: + +Replace multiple anonymous arguments with named arguments: ```purescript -mapArray = _ `map` [1, 2, 3] +square a = a * a +result1 = map (\n -> n * n) [1, 2, 3] ``` +Write the function [as an operator](https://github.com/purescript/documentation/blob/master/language/Syntax.md#functions-as-operators) using backticks: +```purescript +result2 = map (_ `div` 2) [1, 2, 3] +``` +or simply use the corresponding infix operator if one is available: +```purescript +result2 = map (div _ 2) [1, 2, 3] +``` + +Replace the anonymous argument with a named argument: +```purescript +result3 = map (\n -> 1 + n / 2) [1, 2, 3] +``` ## Notes -- While `_ + _` will give this error; `\a -> a + _` will not +- While `(_ + _)` will give this error; `\a -> (a + _)` will not - If you really want to have multiple anonymous arguments, it can be achieved like this: ```purescript