# Confusion with eachrow and pipe

**URL:** <https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237>\
**Category:** New to Julia\
**Created:** [July 24, 2021, 3:34pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237 "2021-07-24T15:34:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [July 24, 2021, 3:34pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/1 "2021-07-24T15:34:40Z")

</div>

Let’s say we have a simple 2D matrix

```julia
a = [0 1 2 3 4; 
    5 6 7 8 9; 
    10 11 12 13 14;
    15 16 17 18 19]

```

## map

It looks like you can apply a function to rows and columns like this

```Julia
julia> map(sum, eachrow(a))
4-element Vector{Int64}:
 10
 35
 60
 85

```

and you get the expected results.

## list comprehension

You can also use a list comprehension like

```julia
julia> [sum(x) for x in eachrow(a)]
4-element Vector{Int64}:
 10
 35
 60
 85

```

## pipe

but if you try to achieve that using pipes then that doesn’t work and seemingly gives you the result as if you’d asked for sums of columns

```julia
a |> eachrow |> sum
5-element Vector{Int64}:
 30
 34
 38
 42
 46

```

and instead using `eachcol` gives you the _row_ sums

```Julia
julia> a |> eachcol |> sum
4-element Vector{Int64}:
 10
 35
 60
 85

```

What’s all that about?

As a relative beginner, these results are quite confusing. This doesn’t seem like it makes sense.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 24, 2021, 4:07pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/2 "2021-07-24T16:07:53Z")

</div>

If you broadcast the piped sum over each row it seems to work alright:

```julia
a |> eachrow |> x -> sum.(x)
# equal to: sum(a,dims=2)

```

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [July 24, 2021, 4:14pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/3 "2021-07-24T16:14:41Z")

</div>

Thanks @rafael.guerra Im my mental model, the sum is receiving a rows, one at a time, so not sure why you would need to use broadcasting in that case.

Can you offer any insights into why the naive pipe approach did not work, or why it seemed to be operating on columns and not rows?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 24, 2021, 4:35pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/5 "2021-07-24T16:35:29Z")

</div>

`eachrow(a)` is an iterator of vectors, and `+` is defined for vectors. So `sum` is applying `+` to an accumulator.

```julia
julia> [1, 2] + [3, 4]
2-element Vector{Int64}:
 4
 6

julia> sum([[1, 2], [3, 4]]) # vector of vectors kind of like eachrow
2-element Vector{Int64}:
 4
 6

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 24, 2021, 4:40pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/6 "2021-07-24T16:40:10Z")

</div>

Should do `collect(eachrow(a))` to see the vector of vectors that is piped into sum.

---

<div class="post-metadata">

**Author:** ![DorianT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doriant/32/24897_2.png) [@DorianT](https://discourse.julialang.org/u/DorianT)\
**Post date:** [July 24, 2021, 4:42pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/7 "2021-07-24T16:42:24Z")

</div>

My best take on this would be to have a look what the iterator created by “eachrow” does yields:

```julia
julia> b = collect.(eachrow(a))
4-element Vector{Vector{Int64}}:
 [0, 1, 2, 3, 4]
 [5, 6, 7, 8, 9]
 [10, 11, 12, 13, 14]
 [15, 16, 17, 18, 19]

```

If you collect the result you see that you get a vector of vectors. Now you can check what happens if you apply sum to such an object:

```julia
julia> sum(b)
5-element Vector{Int64}:
 30
 34
 38
 42
 46

```

And you can check which function is called for it:

```julia
julia> @which sum(b)
sum(a::AbstractArray; dims, kw...) in Base at reducedim.jl:873

```

---

<div class="post-metadata">

**Author:** ![drbenvincent](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drbenvincent/32/3046_2.png) [@drbenvincent](https://discourse.julialang.org/u/drbenvincent)\
**Post date:** [July 24, 2021, 4:49pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/8 "2021-07-24T16:49:46Z")

</div>

Thanks @DorianT. So in short `eachrow` is not generating vectors as such, but a generator which pumps out vectors?

But it’s not immediately clear why what `sum` is being provided with is different when doing `a |> eachcol |> sum` and `[sum(x) for x in eachrow(a)]`? I think that’s what I’m finding particularly confusing.

As in, why the need for `collect` when using pipes, but not when using a list comprehension?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [July 24, 2021, 5:08pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/9 "2021-07-24T17:08:59Z")

</div>

Sum gets the whole iterator at once and sums the elements in the pipe version. In your list comprehension you apply one sum to each element of the iterator. You can use `.|> sum` though

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 24, 2021, 5:17pm UTC](https://discourse.julialang.org/t/confusion-with-eachrow-and-pipe/65237/10 "2021-07-24T17:17:50Z")

</div>

Let’s say the rows of `A` are `r1`, `r2`, `r3`. Then the following have the same meaning:

```julia
A |> eachrow |> sum

sum(eachrow(A))

sum([r1, r2, r3]) # Sum of a list of 3 elements (each element being a vector)

r1 + r2 + r3

```

Your list comprehension does something else… These are equivalent:

```julia
[sum(r) for r in eachrow(A)] # Calculate sum(r) for each row r

[sum(r1), sum(r2), sum(r2)]

```

Here instead of calculating the sum of three arrays we calculate three sums!
