# De-structure results from broadcasting

**URL:** <https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721>\
**Category:** General Usage\
**Tags:** broadcasting\
**Created:** [September 24, 2021, 6:07pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721 "2021-09-24T18:07:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)\
**Post date:** [September 24, 2021, 6:07pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721/1 "2021-09-24T18:07:48Z")

</div>

Hello!

When not using broadcasting, there is a way to destructure results from a function:

```julia
julia> f(x) = (x, x^2);

julia> (y1, y2) = f(5);

julia> y1
5

julia> y2
25

```

However, this does not work while broadcasting:

```julia
julia> x = 1:10
1:10

julia> (z1, z2) = f.(x);

julia> z1
(1, 1)

julia> z2
(2, 4)

```

Is there any way to do the same, and have in `z1` and `z2` arrays with the collection of the first and second arguments of the result respectively?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [September 24, 2021, 6:15pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721/2 "2021-09-24T18:15:20Z")

</div>

Sadly, no. The feature you want is `unzip`. There is a PR [here](https://github.com/JuliaLang/julia/pull/33515) which has not gotten merged into Base.

There is a package for this, though: [Unzip.jl](https://github.com/bramtayl/Unzip.jl).

---

<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:** [September 24, 2021, 7:03pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721/3 "2021-09-24T19:03:58Z")

</div>

> [@nandoconde](#):
>
> `f.(x)`

notice this produces:

```julia
julia> f.(1:10)
10-element Vector{Tuple{Int64, Int64}}:
 (1, 1)
 (2, 4)
 (3, 9)
 (4, 16)
 (5, 25)
 (6, 36)
 (7, 49)
 (8, 64)
 (9, 81)
 (10, 100)

```

which is not ideal storage in memory to begin with if you’re interested in the “columns” (in this display). you can do:

```julia
julia> [x^y for x in 1:10, y in 1:2]
10×2 Matrix{Int64}:
  1 1
  2 4
  3 9
  4 16
  5 25
  6 36
  7 49
  8 64
  9 81
 10 100

```

Sadly, there’s no syntax for multi dimensional comprehension (probably would be too confusing)

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 24, 2021, 7:39pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721/4 "2021-09-24T19:39:30Z")

</div>

You can also do this with `StructArrays.jl`:

```julia
julia> using StructArrays

julia> using StructArrays: components

julia> z1, z2 = components(StructArray(f.(1:10)))
([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 4, 9, 16, 25, 36, 49, 64, 81, 100])

```

edit: this naturally suggests

```julia
unzip(f, x) = components(StructArray(f(xᵢ...) for xᵢ in x))

```

which works well for multidimensional iterators:

```julia
julia> g(x, y) = x*y, y/x
g (generic function with 1 method)

julia> v1, v2 = unzip(g, Iterators.product(1:3, 1:4))
([1 2 3 4; 2 4 6 8; 3 6 9 12], [1.0 2.0 3.0 4.0; 0.5 1.0 1.5 2.0; 0.3333333333333333 0.6666666666666666 1.0 1.3333333333333333])

julia> v1
3×4 Matrix{Int64}:
 1 2 3 4
 2 4 6 8
 3 6 9 12

```

---

<div class="post-metadata">

**Author:** ![JonasWickman](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JonasWickman](https://discourse.julialang.org/u/JonasWickman)\
**Post date:** [September 24, 2021, 8:30pm UTC](https://discourse.julialang.org/t/de-structure-results-from-broadcasting/68721/5 "2021-09-24T20:30:27Z")

</div>

> [@nandoconde](#):
>
> ```julia
> x = 1:10
> 1:10
> 
> julia> (z1, z2) = f.(x)
> 
> ```

There is also Destruct.jl:

```julia
using Destruct
f(x) = (x, x^2)
x = 1:10
z1, z2 = f.(x) |> destruct

```

This yields

```julia
z1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
z2 = [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

```
