Skip to content

Manual: anonymous function in pipeline needs parentheses #43661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 5, 2022

Conversation

tfiers
Copy link
Contributor

@tfiers tfiers commented Jan 4, 2022

Consider:

sos = (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:

sos = (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.

The goal is to avoid befuddlement of users who expect the syntax of the first example to mean that of the second.



As an aside, this confusion is also present in the docstring for |>:

Applies a function to the preceding argument. This allows for easy function chaining.

[1:5;] |> x->x.^2 |> sum |> inv

This is parsed as

[1:5;] |> x -> (x.^2 |> sum |> inv)

but it feels like the author meant

[1:5;] |> (x->x.^2) |> sum |> inv

.

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)
```.
@aviatesk aviatesk merged commit 40d3596 into JuliaLang:master Jan 5, 2022
@tfiers tfiers deleted the patch-1 branch January 5, 2022 14:54
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
…3661)

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)
```.
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
…3661)

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)
```.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants