# Argument destructuring and anonymous functions

**URL:** <https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893>\
**Category:** General Usage\
**Created:** [June 3, 2019, 2:52pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893 "2019-06-03T14:52:49Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tanders/32/6917_2.png) [@tanders](https://discourse.julialang.org/u/tanders)\
**Post date:** [June 3, 2019, 2:52pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/1 "2019-06-03T14:52:49Z")

</div>

Dear all,

It seems that argument destructuring ([Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/index.html#Argument-destructuring-1)) works for named but not anonymous functions (see examples below). Is there a reason for that, or am I just missing something as a newbie?

Thanks a lot!

```julia
# Works
f1((x1, x2)) = x1 + x2
f1([1, 2])

# Works
function f2((x1, x2))
    x1 + x2
end
f2([1, 2])

# Does not work
(((x1, x2)) -> x1 + x2)([1, 2])

# Does not work
function f4()
    function ((x1, x2))
        x1 + x2
    end
end
f4()([1, 2])

```

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [June 3, 2019, 2:57pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/2 "2019-06-03T14:57:40Z")

</div>

It is working, but syntax is a bit different:

```julia
(((x1,x2),)->x1+x2)([1,2])

```

Otherwise the tuple will not be recognized.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [June 3, 2019, 3:19pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/3 "2019-06-03T15:19:26Z")

</div>

Another way of thinking about this is that `()` is not the `tuple` operator, `,` is. Adding more parentheses to `(x, y) -> x + y` to get `((x, y)) -> x + y` doesn’t change anything, because in general adding more parentheses to an expression in Julia doesn’t change what that expression means. If you want to express that the first argument is itself a tuple, you need that extra comma: `((x, y),) -> x + y`.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 3, 2019, 4:35pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/4 "2019-06-03T16:35:00Z")

</div>

It is an unfortunate ugly case when there is just one parameter, and it is a tuple. If there are two parameters that are both tuples, the notation is the one I would expect:

```julia
julia> a = repeat([(1, 2)], 4);
julia> b = repeat([(3, 4)], 4);
map((((x, y), (i, j)) -> (println(x, y, i, j))), a, b);

```

I have experience in Haskell, so I get confused all the time I am mapping over a single tuple and things do not work as I expected (i.e., without needing the external parenthesis with trailing comma). However, Haskell does not have this problem because:

1. [The language itself does not provide a single element tuple.](https://stackoverflow.com/questions/4997741/why-haskell-doesnt-have-a-single-element-tuple)
2. [The language provides an extension where the trailing comma have a complete different meaning (it creates a function which takes parameters to fill the empty spaces between commas and commas, or commas and parenthesis).](https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/guide-to-ghc-extensions/basic-syntax-extensions#tuplesections)
3. [In Julia, the Tuple type is tied to the concept of the arguments of a method/function/“clojure body”,](https://docs.julialang.org/en/v1/manual/types/index.html#Tuple-Types-1) so the one-element tuple is necessary because there are functions that take a single argument. This connection of concepts (parameters list and Tuple type) does not exist in Haskell (AFAIK).

---

<div class="post-metadata">

**Author:** ![tanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tanders/32/6917_2.png) [@tanders](https://discourse.julialang.org/u/tanders)\
**Post date:** [June 3, 2019, 6:31pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/5 "2019-06-03T18:31:18Z")

</div>

Thanks to you all for your kind and helpful feedback!

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [June 3, 2019, 7:38pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/6 "2019-06-03T19:38:57Z")

</div>

> [@Henrique\_Becker](#):
>
> It is an unfortunate ugly case when there is just one parameter, and it is a tuple.

It is unfortunate, since one of the main usecases for tuple destructuring is crafting pipelines, exactly a case where having multiple arguments unpack from a single value can be very useful!

On the other hand, at least it’s not _too_ ugly, and, in my opinion, it’s better than special-casing the parentheses syntax for one-line anonymous functions. I can’t think of a preferable solution, and at least it’s only one comma’s worth of ugliness.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 3, 2019, 8:12pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/7 "2019-06-03T20:12:16Z")

</div>

One thing that helped me a little was to notice that `map` can take a variable number of streams to operate on (again differently from Haskell, if I remember right). Many times I was zipping many streams into a stream of tuples to then passing the stream of tuples to a map with an anonymous function that deconstructed each tuple to operate over its fields, when instead I could just pass the multiple streams as arguments to map (and deconstruct nothing in the anonymous function passed to map, but just take multiple parameters).

---

<div class="post-metadata">

**Author:** ![ninjaaron](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninjaaron/32/6392_2.png) [@ninjaaron](https://discourse.julialang.org/u/ninjaaron)\
**Post date:** [June 3, 2019, 9:18pm UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/8 "2019-06-03T21:18:49Z")

</div>

Thanks for the reminder. I remember having seen that before, but I’d forgotten!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 4, 2019, 7:16am UTC](https://discourse.julialang.org/t/argument-destructuring-and-anonymous-functions/24893/9 "2019-06-04T07:16:15Z")

</div>

And now, fortunately, that functionality is also coming to `mapreduce` in v1.2.
