# Mapreduce with function that has multiple return values

**URL:** <https://discourse.julialang.org/t/mapreduce-with-function-that-has-multiple-return-values/41674>\
**Category:** General Usage\
**Created:** [June 18, 2020, 3:56pm UTC](https://discourse.julialang.org/t/mapreduce-with-function-that-has-multiple-return-values/41674 "2020-06-18T15:56:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)\
**Post date:** [June 18, 2020, 3:56pm UTC](https://discourse.julialang.org/t/mapreduce-with-function-that-has-multiple-return-values/41674/1 "2020-06-18T15:56:02Z")

</div>

I frequently run into an issue where I want to apply a function over a range of inputs and combine the output into arrays, where the function returns more than 1 value, and each return value should be collected in a different array.

If the function returns only a single argument, it is easy to use `mapreduce` with, e.g. `vcat`, to concatenate everything into a single array. Is there a built-in way to do the same for multiple return values (e.g. some kind of `vcat_of_tuple`)?

---

<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 18, 2020, 4:16pm UTC](https://discourse.julialang.org/t/mapreduce-with-function-that-has-multiple-return-values/41674/2 "2020-06-18T16:16:12Z")

</div>

You can broadcast `vcat` itself to apply it to each element of a tuple:

```julia
julia> vcat_of_tuple = (args...) -> vcat.(args...)
#7 (generic function with 1 method)

```

```julia
julia> mapreduce(x -> (x, 2x), vcat_of_tuple, [1, 2, 3])
([1, 2, 3], [2, 4, 6])

```

This might not be the most efficient approach, though, since repeatedly calling `vcat` will allocate lots of temporary arrays:

```julia
julia> @btime mapreduce(x -> (x, 2x), $vcat_of_tuple, $([1, 2, 3]))
  2.139 μs (36 allocations: 1.69 KiB)

```

Instead, you can use `mapfoldl` and `push!` to re-use the resulting array:

```julia
julia> @btime mapfoldl(x -> (x, 2x), (args...) -> push!.(args...), $([1, 2, 3]), init=(Int[], Int[]))
  102.731 ns (8 allocations: 384 bytes)
([1, 2, 3], [2, 4, 6])

```

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 18, 2020, 4:22pm UTC](https://discourse.julialang.org/t/mapreduce-with-function-that-has-multiple-return-values/41674/3 "2020-06-18T16:22:38Z")

</div>

You could steal one of the `unzip` implementations from [https://github.com/JuliaLang/julia/pull/33515](https://github.com/JuliaLang/julia/pull/33515) . (I thought there was a package too but can’t find it.)
