Question

Why does andThen only exist for single argument functions in Scala?

The following code works:

val double = (x: Int) => x * 2
val timesFour = double andThen double

But why is there no andThen method for multi argument functions?

val multiply = (x: Int, y: Int) => x * y
val multiplyAndDouble = multiply andThen double

<console>:10: error: value andThen is not a member of (Int, Int) => Int

Surely it is trivial to add this method. Is there a reason it been omitted from the standard library?

Was it helpful?

Solution

I have just noticed it is easy to work around with the following:

val multiplyAndDouble = multiply.tupled andThen double
val res = multiplyAndDouble(1, 3) // res = 6

OTHER TIPS

I can't speak as to why Function2 doesn't supply and andThen, but Scalaz defines Functor instances for functions of various arities where map is equivalent to andThen, meaning you could write

val multiplyAndDouble = multiply map double

There is a similar question here: Scala API 2.10.*: Function2.andThen what happened to?, but there is also no answer. In my opinion it is possible. Here is working example for Scala 2.11.1:

object TestFunction2 {

  def main(args: Array[String]): Unit = {
    val double = (x: Int) => x * 2
    val timesFour = double andThen double
    println(timesFour(2)) // prints 8

    val multiply = (x: Int, y: Int) => x * y
    val multiplyAndDouble = multiply andThen double
    println(multiplyAndDouble(1, 3)) // prints 6
  }

  implicit def toFunc2(function2: Function2[Int, Int, Int]): Func2[Int, Int, Int] = {
    new Func2[Int, Int, Int] {
      def apply(v1: Int, v2: Int): Int = function2(v1, v2)
    }
  }
}

trait Func2[-T1, -T2, +R] extends Function2[T1, T2, R] {
  def andThen[A](g: R => A): (T1, T2) => A = { (x, y) => g(apply(x, y)) }
}

Another way to write theons's answer is to use:

val multiplyAndDouble = double compose multiply.tupled
val result = multiplyAndDouble(2, 6) // res 24
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top