https://github.com/JuliaLang/julia
Revision 40d3596b0d1e736a201b2dc209c01d91c9e467b6 authored by Tomas Fiers on 05 January 2022, 01:36:42 UTC, committed by GitHub on 05 January 2022, 01:36:42 UTC
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)
```.
1 parent c61cd12
History
Tip revision: 40d3596b0d1e736a201b2dc209c01d91c9e467b6 authored by Tomas Fiers on 05 January 2022, 01:36:42 UTC
Manual: anonymous function in pipeline needs parentheses (#43661)
Tip revision: 40d3596
File Mode Size
.buildkite
.devcontainer
.github
base
cli
contrib
deps
doc
etc
src
stdlib
test
.clang-format -rw-r--r-- 3.3 KB
.codecov.yml -rw-r--r-- 52 bytes
.gitattributes -rw-r--r-- 65 bytes
.gitignore -rw-r--r-- 433 bytes
.mailmap -rw-r--r-- 11.1 KB
CITATION.bib -rw-r--r-- 513 bytes
CITATION.cff -rw-r--r-- 942 bytes
CONTRIBUTING.md -rw-r--r-- 22.5 KB
HISTORY.md -rw-r--r-- 333.2 KB
LICENSE.md -rw-r--r-- 1.3 KB
Make.inc -rw-r--r-- 49.3 KB
Makefile -rw-r--r-- 25.6 KB
NEWS.md -rw-r--r-- 9.9 KB
README.md -rw-r--r-- 7.4 KB
THIRDPARTY.md -rw-r--r-- 3.7 KB
VERSION -rw-r--r-- 10 bytes
julia.spdx.json -rw-r--r-- 35.0 KB
sysimage.mk -rw-r--r-- 4.0 KB

README.md

back to top