# "do" notation over tuples

**URL:** <https://discourse.julialang.org/t/do-notation-over-tuples/14379>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 31, 2018, 6:17pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379 "2018-08-31T18:17:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [August 31, 2018, 6:17pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/1 "2018-08-31T18:17:51Z")

</div>

What am I doing wrong here?

```julia
julia> inds = [(i,j) for i in 1:3, j in 1:5]
3×5 Array{Tuple{Int64,Int64},2}:
 (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)

julia> map(((i,j),) -> i+j, inds)
3×5 Array{Int64,2}:
 2 3 4 5 6
 3 4 5 6 7
 4 5 6 7 8

julia> map(inds) do ((i,j),) i+j end
ERROR: BoundsError: attempt to access 1
  at index [2]
Stacktrace:
 [1] indexed_iterate(::Int64, ::Int64, ::Nothing) at ./tuple.jl:69
 [2] (::getfield(Main, Symbol("##94#95")))(::Tuple{Int64,Int64}) at ./REPL[66]:1
 [3] iterate at ./generator.jl:47 [inlined]
 [4] _collect at ./array.jl:632 [inlined]
 [5] collect_similar(::Array{Tuple{Int64,Int64},2}, ::Base.Generator{Array{Tuple{Int64,Int64},2},getfield(Main, Symbol("##94#95"))}) at ./array.jl:561
 [6] map(::Function, ::Array{Tuple{Int64,Int64},2}) at ./abstractarray.jl:1995
 [7] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [August 31, 2018, 6:20pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/2 "2018-08-31T18:20:05Z")

</div>

```julia
julia> map(inds) do (i,j) i+j end
3×5 Array{Int64,2}:
 2 3 4 5 6
 3 4 5 6 7
 4 5 6 7 8

```

The syntax for `do` notation is a little confusing — it takes arguments as `x, y, z` and not `(x, y, z)`. It’s clearer if we look at this on multiple lines:

```julia
map(inds) do i, j
   i+j
end

```

The version above takes two arguments called `i` and `j`;

```julia
map(inds) do (i, j)
   i+j
end

```

This one destructures a single argument into a tuple.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [August 31, 2018, 6:21pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/3 "2018-08-31T18:21:46Z")

</div>

Oh weird, could have sworn I’d already tried that. Thank you!

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 31, 2018, 6:44pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/4 "2018-08-31T18:44:17Z")

</div>

Why not just sum?

```julia
julia> inds = [(i,j) for i in 1:3, j in 1:5]
3×5 Array{Tuple{Int64,Int64},2}:
 (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)

julia> sum.(inds)
3×5 Array{Int64,2}:
 2 3 4 5 6
 3 4 5 6 7
 4 5 6 7 8

```

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [August 31, 2018, 6:53pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/5 "2018-08-31T18:53:41Z")

</div>

> [@Seif\_Shebl](#):
>
> Why not just sum?

What I posted isn’t the real problem. For a DSL I made a `For` that works like `map`, so I can write things like

```julia
For(inds) do (i,j)
    f(i,j)
end

```

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [August 31, 2018, 7:26pm UTC](https://discourse.julialang.org/t/do-notation-over-tuples/14379/6 "2018-08-31T19:26:45Z")

</div>

Aha, OK I didn’t imagine that, so @yurivish reply is good enough for this.
