Tupled function outputs

functional-programming, scala, scalaz

Solution

You're in luck, scalaz (7) does have this with `&&&`:

  import scalaz._
  import Scalaz._

  val intToString = (i:Int) => i.toString
  val intPlusTwo = (i:Int) => i + 2

  val combined = intToString &&& intPlusTwo

  println(combined(1)) // (1, 3)

And you can continue to combine though it does build up tuples per what your comments would suggest:

  val combinedMore = intToString &&& intPlusTwo &&& intToString

  println(combinedMore(1)) // ((1,3),1)

Problem

I'm looking for a function that takes a tuple of functions over a common domain and returns a function from that domain to a tuple of their respective outputs. I'm assuming that such a utility is either built into Scala or is tucked away somewhere in Scalaz, but I have been unable to find it. For example, the special case of a pair of functions (and taking the functions as individual arguments rather than a pair) would look like: ``` def pairFunc[I, O1, O2](f: I => O1, g: I => O2): I => (O1, O2) = (x: I) => (f(x), g(x)) ``` Is there a way to achieve this for an arbitrary-arity tuple of functions? EDIT: A method on a Function type whose output looks like `X -> ((A, B), C)` and whose construction looks like `f fZip g fZip h` is just as fine as one a function whose output is `X -> (A, B, C)`.

Original source

Related problems