柯里化概念及声明方式

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 = 

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 = 

scala> func(3)
res11: Int = 6

你可能感兴趣的:(柯里化概念及声明方式)