# How to sum tuple elements

**URL:** <https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 3, 2021, 3:52pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753 "2021-08-03T15:52:57Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 3:52pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/1 "2021-08-03T15:52:57Z")

</div>

`x` is a matrix 5x5 of tuples. How can I filter the tuples where the sum of their elements is 6?

```julia
numbers = [1,2,3,4,5]
x = collect(Iterators.product(numbers, numbers))

```

---

<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:** [August 3, 2021, 3:54pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/2 "2021-08-03T15:54:29Z")

</div>

Maybe

```julia
julia> x = collect(Iterators.product(numbers, numbers))
5×5 Matrix{Tuple{Int64, Int64}}:
 (1, 1) (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 1) (2, 2) (2, 3) (2, 4) (2, 5)
 (3, 1) (3, 2) (3, 3) (3, 4) (3, 5)
 (4, 1) (4, 2) (4, 3) (4, 4) (4, 5)
 (5, 1) (5, 2) (5, 3) (5, 4) (5, 5)

julia> (x -> sum(x) .== 6).(x)
5×5 BitMatrix:
 0 0 0 0 1
 0 0 0 1 0
 0 0 1 0 0
 0 1 0 0 0
 1 0 0 0 0

```

?

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 4:06pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/3 "2021-08-03T16:06:47Z")

</div>

OK. But how can filter the tuples that have value `1`? Like (1, 5), (2, 4), …  
I tried to use:  
`y = filter((x -> sum(x) .== 6 .x), x)`  
but I got an error.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 3, 2021, 4:15pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/4 "2021-08-03T16:15:20Z")

</div>

```julia
julia> x = [ (1,2) (2,4)
             (1,1) (1,5) ]
2×2 Matrix{Tuple{Int64, Int64}}:
 (1, 2) (2, 4)
 (1, 1) (1, 5)

julia> filter(t -> sum(t) == 6, x)
2-element Vector{Tuple{Int64, Int64}}:
 (2, 4)
 (1, 5)

```

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 4:25pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/5 "2021-08-03T16:25:42Z")

</div>

@leandromartinez98 Sorry, your solution gives an error.  
I don’t understand why the complete solution gives an error:

```julia
numbers = [1,2,3,4,5]
x = collect(Iterators.product(numbers, numbers))
y = filter(t -> sum(t) == 6, x)

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 3, 2021, 4:39pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/6 "2021-08-03T16:39:06Z")

</div>

It doesn’t here, just copying and pasting your code:

```julia
julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.6.2 (2021-07-14)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> numbers = [1,2,3,4,5]
5-element Vector{Int64}:
 1
 2
 3
 4
 5

julia> x = collect(Iterators.product(numbers, numbers))
5×5 Matrix{Tuple{Int64, Int64}}:
 (1, 1) (1, 2) (1, 3) (1, 4) (1, 5)
 (2, 1) (2, 2) (2, 3) (2, 4) (2, 5)
 (3, 1) (3, 2) (3, 3) (3, 4) (3, 5)
 (4, 1) (4, 2) (4, 3) (4, 4) (4, 5)
 (5, 1) (5, 2) (5, 3) (5, 4) (5, 5)

julia> y = filter(t -> sum(t) == 6, x)
5-element Vector{Tuple{Int64, Int64}}:
 (5, 1)
 (4, 2)
 (3, 3)
 (2, 4)
 (1, 5)

julia> 

```

---

<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:** [August 3, 2021, 4:48pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/7 "2021-08-03T16:48:01Z")

</div>

If you `filter`, what kind of shape do you expect the object to have? It’s a matrix, what does it mean to just get rid of elements in the matrix?

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 4:58pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/8 "2021-08-03T16:58:22Z")

</div>

> [@sbacelar](#):
>
> `y = filter(t -> sum(t) == 6, x)`

Sorry, you’re right. I must have done something wrong.  
It is OK in the REPL but in Vscode gives the error `MethodError: objects of type Int64 are not callable`.

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 4:59pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/9 "2021-08-03T16:59:32Z")

</div>

You’re right. But I am only interested in some elements of the matrix and I want to list them.

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 3, 2021, 5:06pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/10 "2021-08-03T17:06:07Z")

</div>

FWIW, if you don’t care of a matrix shape and only want the tuples which give the sum 6, then you may rewrite your code without `collect`ing the product:

```julia
julia> numbers = 1:5;

julia> x = Iterators.product(numbers, numbers); # or x = ((k, l) for k in numbers for l in numbers)

julia> ts = [tup for tup in x if sum(tup) == 6]
5-element Vector{Tuple{Int64, Int64}}:
 (5, 1)
 (4, 2)
 (3, 3)
 (2, 4)
 (1, 5)

```

The same can be written as `ts = collect(Iterators.filter(tup -> sum(tup) == 6, x))` but doesn’t work for eager `filter` (looks like `filter` isn’t defined for lazy iterables).

---

<div class="post-metadata">

**Author:** ![cchderrick](https://avatars.discourse-cdn.com/v4/letter/c/ecd19e/32.png) [@cchderrick](https://discourse.julialang.org/u/cchderrick)\
**Post date:** [August 3, 2021, 5:25pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/11 "2021-08-03T17:25:54Z")

</div>

Here is what I learnt, where an error said some type “are not callable”, there is somewhere in your code you used a function name as variable.

For example:

```julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.4.2 (2020-05-23)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> sum = 6
6

julia> sum([1,2])
ERROR: MethodError: objects of type Int64 are not callable
Stacktrace:
 [1] top-level scope at REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 5:34pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/12 "2021-08-03T17:34:47Z")

</div>

Thank you. I had a variable called `sum` in `Vscode` but not in REPL.

---

<div class="post-metadata">

**Author:** ![sbacelar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbacelar/32/4359_2.png) [@sbacelar](https://discourse.julialang.org/u/sbacelar)\
**Post date:** [August 3, 2021, 5:39pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/13 "2021-08-03T17:39:24Z")

</div>

What do you mean for `eager filter`?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [August 3, 2021, 8:00pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/14 "2021-08-03T20:00:43Z")

</div>

`Base.filter` is eager, i.e. it produces all the values once called.  
`Iterators.filter` is lazy, i.e. it produces the values one by one on demand.

Because of that, `Base.filter` is not defined for an arbitrary iterable, only for certain types which are guaranteed to have a finite length. And, as it turns out, it doesn’t work with generators. `Iterators.filter` works with any iterable because of laziness.

So, this works:

```julia
nums = (n for n in 1:10)
evens = [n for n in nums if iseven(n)]

```

This stops with an error because `Base.filter` doesn’t work with generators (because they potentially may produce an infinite sequence):

```julia
filter(iseven, nums)

```

---

<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:** [August 3, 2021, 8:52pm UTC](https://discourse.julialang.org/t/how-to-sum-tuple-elements/65753/15 "2021-08-03T20:52:59Z")

</div>

FWIW, some benchmarks with the classical Matlab-like alternative:

```julia
using BenchmarkTools
f(x,n) = x[sum.(x) .== n]
g(x,n) = filter(t -> sum(t) == n, x)

numbers = 1:100
x = collect(Iterators.product(numbers, numbers))
n = 100
f(x,n) == g(x,n) # true

@btime f($x,$n) # 4.4 μs (4 allocations: 7.3 KiB)
@btime g($x,$n) # 8.7 μs (3 allocations: 156.3 KiB)

```
