Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Function2 have an andThen method?

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?

like image 583
theon Avatar asked Feb 10 '14 14:02

theon


2 Answers

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
like image 189
Hugh Avatar answered Sep 27 '22 20:09

Hugh


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
like image 44
theon Avatar answered Sep 27 '22 19:09

theon