# ( (x,y) -\> x+y ).( \[ (1,2), (3,4) \] )

**URL:** https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846
**Category:** General Usage
**Tags:** tuple, vector, functions
**Created:** [September 8, 2021, 5:09am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846 "2021-09-08T05:09:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 8, 2021, 5:09am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/1 "2021-09-08T05:09:41Z")

</div>

` ( (x,y) -> x+y ).( [(1,2), (3,4)] )`

gives an error.  
Is it possible to apply a multi-variable anonymous function to a vector of tuples like this ?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 8, 2021, 5:14am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/2 "2021-09-08T05:14:05Z")

</div>

You’re getting an error because your anonymous function takes two arguments, whereas each tuple is only a single argument.

You can use this as the anonymous function instead:

```julia
(x -> x[1] + x[2])

```

Or simply use `sum`.

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [September 8, 2021, 5:25am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/3 "2021-09-08T05:25:37Z")

</div>

You can use destructuring in the function argument list as follows:

```julia
julia> ( ((x,y),) -> x+y ).( [(1,2), (3,4)] )
2-element Vector{Int64}:
 3
 7

```

---

<div class="post-metadata">

### Author: ![Lincoln\_Hannah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lincoln_hannah/32/19198_2.png) [@Lincoln\_Hannah](https://discourse.julialang.org/u/Lincoln_Hannah)
#### Post date: [September 8, 2021, 5:33am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/4 "2021-09-08T05:33:39Z")

</div>

> [@c42f](#):
>
> `( ((x,y),) -> x+y ).( [(1,2), (3,4)] )`

Thanks. - always have to add that comma and put in another backet 🙂

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [September 8, 2021, 5:34am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/5 "2021-09-08T05:34:35Z")

</div>

Heh, whether you _should_ do this is another matter of course. It’s pretty hard to read 🙂

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [September 8, 2021, 5:34am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/6 "2021-09-08T05:34:40Z")

</div>

can you point out where that is documented?

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [September 8, 2021, 5:38am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/7 "2021-09-08T05:38:48Z")

</div>

> [@Lincoln\_Hannah](#):
>
> Is it possible to apply a multi-variable anonymous function to a vector of tuples like this ?

No. But an anonymous function of a tuple can be applied to a vector of tuples as follows:

```julia
jula> (((x, y),) -> x + y).([(1, 2), (3, 4)])
2-element Vector{Int64}:
 3
 7

```

This is appoximately equivalent to

```julia
julia> f((x, y)) = x + y
julia> f.([(1, 2), (3, 4)])
2-element Vector{Int64}:
 3
 7

```

Note that the comma immediately after `(x, y)` is not necessary in this case, but it is necessary in the case of the anonymous function above.

You can also get the same result with

```julia
julia> (t -> +(t...)).([(1, 2), (3, 4)])
2-element Vector{Int64}:
 3
 7

```

Edit: A few minutes late; Julia’s people are too quick to answer! 😊

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [September 8, 2021, 5:40am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/8 "2021-09-08T05:40:37Z")

</div>

> [@amrods](#):
>
> can you point out where that is documented?

It’s a consequence of function argument destructuring, combined with anonymous function syntax. Argument destructuring is documented in

[https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring](https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring)

---

<div class="post-metadata">

### Author: ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)
#### Post date: [September 8, 2021, 5:43am UTC](https://discourse.julialang.org/t/x-y-x-y-1-2-3-4/67846/9 "2021-09-08T05:43:14Z")

</div>

> [@genkuroki](#):
>
> an anonymous function of a tuple

Actually argument destructuring is not based on pattern matching, it’s based on iteration. So rather than just working for tuples it works for any iterable which is passed:

```julia
julia> f((x,y)) = x+y
f (generic function with 1 method)

julia> f(Set([1,2]))
3

julia> f([1,2])
3

julia> f((1,2))
3

```
