Nik*_*mar 10 scala anonymous-function currying
如何在Scala中创建匿名和curried功能?以下两个失败:
scala> (x:Int)(y:Int) => x*y
<console>:1: error: not a legal formal parameter
(x:Int)(y:Int) => x*y
^
scala> ((x:Int)(y:Int)) => x*y
<console>:1: error: not a legal formal parameter
((x:Int)(y:Int)) => x*y
^
Run Code Online (Sandbox Code Playgroud)
dre*_*xin 18
要创建一个curried函数,请将其写为多个函数(实际上就是这种情况;-)).
scala> (x: Int) => (y: Int) => x*y
res2: Int => Int => Int = <function1>
Run Code Online (Sandbox Code Playgroud)
这意味着你有一个从Int到Int到Int的函数的函数.
scala> res2(3)
res3: Int => Int = <function1>
Run Code Online (Sandbox Code Playgroud)
或者你可以像这样写:
scala> val f: Int => Int => Int = x => y => x*y
f: Int => Int => Int = <function1>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2282 次 |
| 最近记录: |