# Tuple Restructuring Syntax

**URL:** <https://discourse.julialang.org/t/tuple-restructuring-syntax/114461>\
**Category:** General Usage\
**Created:** [May 19, 2024, 9:25pm UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461 "2024-05-19T21:25:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [May 19, 2024, 9:25pm UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/1 "2024-05-19T21:25:34Z")

</div>

Can someone help me understand why the latter works but the former does not?

```julia
map((x, y) -> x * y, zip(xs, ys)) # MethodError

```

```julia
map(((x, y),) -> x * y, zip(xs, ys)) # works

```

To me the first is akin to the form I usually use:

```Julia
map(zip(xs, ys)) do (x, y) 
    x*y
end

```

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 19, 2024, 9:28pm UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/2 "2024-05-19T21:28:33Z")

</div>

> [@Alec\_Loudenback](#):
>
> To me the first is akin to the form I usually use:
> 
> ```julia
> map(zip(xs, ys)) do (x, y) 
> x*y
> end
> 
> ```

No it’s like

```julia
julia> map(zip(xs, ys)) do x, y
           x*y
       end
ERROR: MethodError: no method matching (::var"#15#16")(::Tuple{Int64, Int64})

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [May 19, 2024, 9:31pm UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/3 "2024-05-19T21:31:15Z")

</div>

`julia> map(splat((x, y) -> x * y), zip(xs, ys)) #works`

or just

```julia
julia> map(*, xs, ys)

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 19, 2024, 11:18pm UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/4 "2024-05-19T23:18:11Z")

</div>

With the first definition, the function takes two arguments. Trying to pass a single tuple to it will not work.

```julia
julia> L = (x, y) -> x * y
#5 (generic function with 1 method)

julia> methods(L)
# 1 method for anonymous function "#5":
 [1] (::var"#5#6")(x, y)
     @ REPL[26]:1

julia> L(1,2)
2

julia> L((1,2))
ERROR: MethodError: no method matching (::var"#5#6")(::Tuple{Int64, Int64})

```

The second method takes a single argument, a tuple of two values.

```julia
julia> M = ((x, y),) -> x * y
#9 (generic function with 1 method)

julia> methods(M)
# 1 method for anonymous function "#9":
 [1] (::var"#9#10")(::Any)
     @ REPL[31]:1

julia> M(1,2)
ERROR: MethodError: no method matching (::var"#9#10")(::Int64, ::Int64)

julia> M((1,2))
2

```

The `MethodError` has nothing to do with destructuring syntax. It’s just about the arguments taken, 1 or 2.

---

<div class="post-metadata">

**Author:** ![Alec\_Loudenback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alec_loudenback/32/278_2.png) [@Alec\_Loudenback](https://discourse.julialang.org/u/Alec_Loudenback)\
**Post date:** [May 20, 2024, 2:57am UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/5 "2024-05-20T02:57:39Z")

</div>

Thanks all so far. Why doesn’t the `do` syntax require the nested tuple definition? E.g. this errors:

```julia
map(zip(xs, ys)) do ((x, y),)
           x*y
end

```

With my current understanding, it seems there’s an inconsistency between the `do` syntax and anonymous function syntax.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [May 20, 2024, 3:25am UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/6 "2024-05-20T03:25:55Z")

</div>

IMO `do x, y` vs `function f((x,y),)` is a little inconsistent, yeah.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 20, 2024, 3:56am UTC](https://discourse.julialang.org/t/tuple-restructuring-syntax/114461/7 "2024-05-20T03:56:57Z")

</div>

> [@Alec\_Loudenback](#):
>
> `map((x, y) -> x * y, zip(xs, ys))`

I might write it as

```julia
map(xs, ys) do x, y
   x * y
end

```

just so I can skip `zip()`
