# Is there something in Julia similar to the as-patterns in Haskell?

**URL:** <https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785>\
**Category:** General Usage\
**Created:** [December 17, 2022, 5:50pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785 "2022-12-17T17:50:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 17, 2022, 5:50pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/1 "2022-12-17T17:50:46Z")

</div>

Hello,  
In Haskell we can write an argument of a function as `pair@(x,y)`. That means that `pair` is the pair with the two elements `x` and `y`. Is there something similar in Julia? For example if I have a list of pairs, I would like to do

```julia
map(pair@(x,y) -> x+y, my_list_of_pairs)

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 17, 2022, 6:02pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/2 "2022-12-17T18:02:05Z")

</div>

Is this what you mean?

```julia
julia> my_list_of_pairs = [1=>2, 3=>4, 5=>6]
3-element Vector{Pair{Int64, Int64}}:
 1 => 2
 3 => 4
 5 => 6

julia> map(((x,y),) -> x+y, my_list_of_pairs)
3-element Vector{Int64}:
  3
  7
 11

```

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 17, 2022, 6:07pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/3 "2022-12-17T18:07:31Z")

</div>

Thanks. No, by “pair” I mean a 2-tuple, e.g. `[(1,2), (3,4), (5,6)]`.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 17, 2022, 6:08pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/4 "2022-12-17T18:08:52Z")

</div>

That same code will still work.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 17, 2022, 6:15pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/5 "2022-12-17T18:15:57Z")

</div>

Ah yes it works, thanks. Why this notation `((x,y),)`?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 17, 2022, 6:23pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/6 "2022-12-17T18:23:56Z")

</div>

A single argument function would be: `(p,) -> p[1]+p[2]`. If we then replace `p` by `(x,y)` that means `x, y = p`, i.e. it iterates over the input argument and assigns to `x` and `y`.

It is also possible to extract elements from a struct with a similar syntax. For example:

```julia
julia> my_list_of_numbers = [1+2im, 3+4im, 5+6im]
3-element Vector{Complex{Int64}}:
 1 + 2im
 3 + 4im
 5 + 6im

julia> map(((;re, im),) -> re+im, my_list_of_numbers)
3-element Vector{Int64}:
  3
  7
 11

```

The above works because `Complex` has the field names `re` and `im`.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 18, 2022, 3:12am UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/7 "2022-12-18T03:12:56Z")

</div>

Nice. And in the same spirit, can we do something like `[x+y for pair@(x,y) in list_of_pairs]`?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [December 18, 2022, 11:02am UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/8 "2022-12-18T11:02:45Z")

</div>

Sure. Just get rid of the `pair@`.

```julia
julia> [x+y for (x,y) in my_list_of_pairs]
3-element Vector{Int64}:
  3
  7
 11

```

---

<div class="post-metadata">

**Author:** ![cjdoris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cjdoris/32/213133_2.png) [@cjdoris](https://discourse.julialang.org/u/cjdoris)\
**Post date:** [December 18, 2022, 11:46am UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/9 "2022-12-18T11:46:41Z")

</div>

This general concept is known as “destructuring”. See here: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#destructuring-assignment).

---

<div class="post-metadata">

**Author:** ![mstewart](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mstewart](https://discourse.julialang.org/u/mstewart)\
**Post date:** [December 18, 2022, 12:01pm UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/10 "2022-12-18T12:01:21Z")

</div>

You seem happy with the solution, but I was wondering if there is some miscommunication going on. Specifically, do you really want or need as patterns? Your example doesn’t actually use `pair` in the function, so it seems like an example of a case where the as pattern isn’t needed and isn’t used. As opposed to some other non-Julia/non-Haskell snippet like:

```julia
map(pair@(x,y) -> x+pair[2], my_list_of_pairs)

```

where you would be using the binding to `pair` established by the as pattern. I don’t think there is anything like an as pattern in Julia or provided in any package I know of. However [MLStyle.jl](https://github.com/thautwarm/MLStyle.jl) is definitely worth checking out for fancy pattern matching.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 19, 2022, 8:45am UTC](https://discourse.julialang.org/t/is-there-something-in-julia-similar-to-the-as-patterns-in-haskell/91785/11 "2022-12-19T08:45:29Z")

</div>

Indeed, in Haskell you can use `pair` as well.
