Vectorize Scala function with multiple return values

scala

Solution

You can unzip it (obviously, it operation which is oposite to zip):

val c = foobar(a,b).unzip
//res2: (s.c.m.IndexedSeq[Int], s.c.m.IndexedSeq[Int]) = (ArrayBuffer(1, 2, 3),ArrayBuffer(4, 5, 6))

But you'll end up with IndexedSeqs, not Arrays.

Problem

What is the most elegant way to vectorize a scala function that returns multiple values? For example, suppose I have the function: ``` def Foobar(foo: Int, bar: Int): (Int, Int) = (foo, bar) ``` If it returned a single value, I would do something like this: ``` val a = Array(1, 2, 3) val b = Array(4, 5, 6) val c = (a,b).zipped.map(foobar) ``` But since it returns a tuple, I end up with an `Array[(Int,Int)]`, whereas I would prefer an `(Array[Int], Array[Int])`. What is the proper way to do something like this? Are there any clever patterns for generalizing this to something like this: ``` val c = vectorized(foobar,a,b) ``` Any ideas would be much appreciated. Thanks!

Original source