# Broadcast multi-valued function over array

**URL:** <https://discourse.julialang.org/t/broadcast-multi-valued-function-over-array/93435>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [January 24, 2023, 1:01am UTC](https://discourse.julialang.org/t/broadcast-multi-valued-function-over-array/93435 "2023-01-24T01:01:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinfreedom](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@jinfreedom](https://discourse.julialang.org/u/jinfreedom)\
**Post date:** [January 24, 2023, 1:01am UTC](https://discourse.julialang.org/t/broadcast-multi-valued-function-over-array/93435/1 "2023-01-24T01:01:52Z")

</div>

I have a multi-valued function, e.g., `(x,y)->F(x,y)=(fx(x,y), fy(x,y))`, where I would like to redefine its broadcast behavior, such that `F.(xArray,yArray)` returns `(fx.(xArray,yArray), fy.(xArray, yArray))`, instead of the standard `Array((fx(x,y),fy(x,y)) for x,y in (xArray,yArray))`. In other words I want tuple of arrays rather than array of tuples as return types. Is there a proper way to redefine the function `F` 's broadcast behavior over all `broadcastables`, including arrays, tuples, etc? I don’t want additional allocations to be involved, e.g., `getindex.(...)`

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 24, 2023, 3:02am UTC](https://discourse.julialang.org/t/broadcast-multi-valued-function-over-array/93435/2 "2023-01-24T03:02:37Z")

</div>

> [@jinfreedom](#):
>
> I want tuple of arrays rather than array of tuples as return types

You could use the [StructArrays.jl package](https://github.com/JuliaArrays/StructArrays.jl), which allows you to construct an array that _acts_ like an array of tuples but is actually a tuple of arrays (which can then be extracted without making a copy). For example:

```julia
julia> using StructArrays

julia> f(x) = (x+1, x+2) # example function returning a tuple
f (generic function with 1 method)

julia> y = StructArray{Tuple{Int, Int}}(undef, 5);

julia> y .= f.(1:5)
5-element StructArray(::Vector{Int64}, ::Vector{Int64}) with eltype Tuple{Int64, Int64}:
 (2, 3)
 (3, 4)
 (4, 5)
 (5, 6)
 (6, 7)

julia> a, b = StructArrays.components(y) # tuple of arrays
([2, 3, 4, 5, 6], [3, 4, 5, 6, 7])

```
