the*_*eon 15 scala function-composition
为什么andThen
只存在Scala中的单个参数函数?
以下代码有效:
val double = (x: Int) => x * 2
val timesFour = double andThen double
Run Code Online (Sandbox Code Playgroud)
但为什么没有andThen
多参数函数的方法呢?
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
Run Code Online (Sandbox Code Playgroud)
当然,添加这种方法是微不足道的.它是否有理由从标准库中省略?
我刚刚注意到以下内容很容易解决:
val double = (x: Int) => x * 2
val timesFour = double andThen double
Run Code Online (Sandbox Code Playgroud)
但我仍然想知道为什么andThen
没有andThen
我刚刚注意到很容易解决以下问题:
val multiplyAndDouble = multiply.tupled andThen double
val res = multiplyAndDouble(1, 3) // res = 6
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1818 次 |
最近记录: |