julia
40d3596b - Manual: anonymous function in pipeline needs parentheses (#43661)

Commit
4 years ago
Manual: anonymous function in pipeline needs parentheses (#43661) Consider: ```julia d = (1:10 .|> x -> x^2 |> sum ) ``` This seems to compute a sum of squares. But it does not. It outputs a vector of squares. (Because `|> sum` is parsed as being part of the anonymous function's body). Parentheses are needed for the desired (expected?) behaviour: ```julia d = (1:10 .|> (x -> x^2) |> sum ) ``` This PR adds a small note and example to the "Function composition and piping" section of the manual to flag this. As an aside, this confusion is even present in the [docstring](https://docs.julialang.org/en/v1/base/base/#Base.:|%3E) for `|>`: > Applies a function to the preceding argument. This allows for easy function chaining. > ```julia [1:5;] |> x->x.^2 |> sum |> inv ``` This is parsed as ```julia [1:5;] |> x-> (x.^2 |> sum |> inv) ``` but it feels like the author meant ```julia [1:5;] |> (x-> x.^2) |> sum |> inv) ```.
Author
Parents
Loading