# Help understanding broadcasted pipe operator

**URL:** <https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 1, 2019, 3:17pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591 "2019-11-01T15:17:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [November 1, 2019, 3:17pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591/1 "2019-11-01T15:17:58Z")

</div>

I would expect the result of

```julia
[1, 2] .|> u->u^2 |> sum

```

to give the answer `5` since

```julia
[1, 2] .|> u->u^2

```

gives

```julia
2-element Array{Int64,1}:
 1
 4

```

and

```julia
[1, 4] |> sum

```

gives `5`.

Instead I am getting

```julia
[1, 2] .|> u->u^2 |> sum

2-element Array{Int64,1}:
 1
 4

```

What’s going on here? It seems like the second pipe is broadcasting over the vector even though I am not using the dot pipe operator.

What’s weird is I can call them separately in the REPL and it works the way I expect it to:

```julia
julia> [1, 2] .|> u->u^2
2-element Array{Int64,1}:
 1
 4

julia> ans |> sum
5

```

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [November 1, 2019, 3:21pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591/2 "2019-11-01T15:21:22Z")

</div>

```julia
julia> ([1, 2] .|> u->u^2) |> sum
5

```

Can’t explain why though

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [November 1, 2019, 3:25pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591/3 "2019-11-01T15:25:48Z")

</div>

I think it gets parsed as `[1, 2] .|> (u->u^2) |> sum)`

```julia
julia> Meta.lower(Main,:([1, 2] .|> u->u^2 |> sum))
 # ...
│ %2 = (Core.svec)(##10#11, Core.Any)
│ %3 = (Core.svec)()
│ %4 = (Core.svec)(%2, %3)
#...
    (%1)()
    (Base.literal_pow)(^, u, %2)
    %3 |> sum
    return %4
end)))
#...

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 1, 2019, 3:37pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591/4 "2019-11-01T15:37:24Z")

</div>

Even simpler than `Meta.lower`, you can just see it by quoting it:

```julia
julia> :([1, 2] .|> u->u^2 |> sum)
:([1, 2] .|> (u->begin
              #= REPL[2]:1 =#
              u ^ 2 |> sum
          end))

```

This is how it works without broadcasting, too:

```julia
julia> :([1, 2] |> u->u^2 |> sum)
:([1, 2] |> (u->begin
              #= REPL[3]:1 =#
              u ^ 2 |> sum
          end))

```

The trick is that the last `|>` is becoming _a part of the anonymous function_! Here’s another fix:

```julia
julia> [1, 2] .|> (u->u^2) |> sum
5

```

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [November 1, 2019, 3:41pm UTC](https://discourse.julialang.org/t/help-understanding-broadcasted-pipe-operator/30591/5 "2019-11-01T15:41:13Z")

</div>

Oh, that makes sense. Thanks!
