scala> def currying(x: Int)(y: Int) = x * y //实现柯里化
currying: (x: Int)(y: Int)Int
scala> currying(3)(4)
res1: Int = 12
scala> val curry = currying(3) _
curry: Int => Int = <function1>
scala> curry(4)
res2: Int = 12
scala> curry(5)
res3: Int = 15
scala> def m1(x: Int, y: Int) = x * y
m1: (x: Int, y: Int)Int
scala> m1(5,10)
res4: Int = 50
scala> def m2(x: Int)(implicit y: Int = 5) = x * y
m2: (x: Int)(implicit y: Int)Int
scala> m2(4)
res5: Int = 20
scala> m2(3)(6)
res6: Int = 18
scala> implicit val x = 100
x: Int = 100
scala> m2(4)
res7: Int = 400
scala> val arr = Array(("scala",1),("scala",2),("scala",3))
arr: Array[(String, Int)] = Array((scala,1), (scala,2), (scala,3))
scala> arr.foldLeft(0)(_+_._2)
res10: Int = 6
scala> def curry(x: Int) = (y: Int) => x * y
curry: (x: Int)Int => Int
scala> val func = curry(2)
func: Int => Int = <function1>
scala> func(3)
res11: Int = 6
柯里化概念及声明方式
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 澜妈想问下魅宝们,是否有过这样的疑惑,为什么同样的饭,闰蜜吃了没反应,自己却在疯狂的长肉?为什么!为什么!!为什么...