# Multiple assignments using broadcasting - any hacks?

**URL:** <https://discourse.julialang.org/t/multiple-assignments-using-broadcasting-any-hacks/16592>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [October 21, 2018, 4:21am UTC](https://discourse.julialang.org/t/multiple-assignments-using-broadcasting-any-hacks/16592 "2018-10-21T04:21:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [October 21, 2018, 4:21am UTC](https://discourse.julialang.org/t/multiple-assignments-using-broadcasting-any-hacks/16592/1 "2018-10-21T04:21:58Z")

</div>

I would like to do something similar to the following:

```julia
f(x) = x+1, x+2

a = Vector{Int}(undef, 10)
b = similar(a)
a, b .= f.(1:10)

```

which of course does not work. For context, I have a trivially parallel operation to perform that I want to at least test as a GPU kernel; each operation produces several resulting values and it would be most natural to store each in its own array. So `a`, `b`, and `1:10` would all be `CuArray`s. I could of course write results to an array-of-structs if there’s not a more elegant solution.

Can anyone think of an elegant away to implement this kind of “destructuring” of a broadcast result that will be performant with `CuArray`s? And please correct me if this is a misconception, but I believe it will not be efficient to just write a `for` loop when working with `CuArray`s, unless I want to get low-level and write code using CUDANative myself.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [October 21, 2018, 5:04am UTC](https://discourse.julialang.org/t/multiple-assignments-using-broadcasting-any-hacks/16592/2 "2018-10-21T05:04:05Z")

</div>

It does kind of work, even ` a,b = f.(1:10)`, but only gives the first two tuples  
There might be a way to nest a function that switches the output format

```julia
a = f.(1:10);
b= map(x->x[1], a); c= map(x->x[2], a);

```

Added: maybe like

```julia
g(x,y) = x[y];
function xgroup(n)
  a = f.(1:10);
  return g.(a,1), g.(a,2);
  end;
```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [October 21, 2018, 8:44am UTC](https://discourse.julialang.org/t/multiple-assignments-using-broadcasting-any-hacks/16592/3 "2018-10-21T08:44:16Z")

</div>

> [@Destructuring and broadcast](https://discourse.julialang.org/t/destructuring-and-broadcast/5672/5):
>
> In v0.6, I used StructsOfArrays to do broadcasting with tuple output: using StructsOfArrays T = Float64 TV = Vector{Float64} a = zeros(T, 10); b = copy(a) s = StructOfArrays{NTuple{2,T}, 1, Tuple{TV, TV}}((a, b)) f(a, b) = (a + b, a \* b) s .= f.(1:10, 1:10) A macro for the above should be simple enough. Figuring out T and TV and handling the case where a and b are not already allocated are the main challenges I can see.
