Why doesn't Function2 have an andThen method?

后端 未结 4 666
南笙
南笙 2021-02-13 04:12

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

The following code works:

val double = (x: Int) => x * 2
val timesFou         


        
相关标签:
4条回答
  • 2021-02-13 04:52

    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
    
    0 讨论(0)
  • 2021-02-13 05:00

    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
    
    0 讨论(0)
  • 2021-02-13 05:01

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

    val multiplyAndDouble = double compose multiply.tupled
    val result = multiplyAndDouble(2, 6) // res 24
    
    0 讨论(0)
  • 2021-02-13 05:16

    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)) }
    }
    
    0 讨论(0)
提交回复
热议问题