Skip to content

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