본문 바로가기

Scala

Curry - Partial function explain

http://stackoverflow.com/questions/8650549/using-partial-functions-in-scala-how-does-it-work/8650639#8650639


언제봐도 어렵고 헷갈리는 이 두가지 개념....



scala> def modN(n: Int, x: Int) = ((x % n) == 0)

modN: (n: Int, x: Int)Boolean


scala> modN(5, _ : Int)

res0: Int => Boolean = <function1>


scala> res0(10)

res2: Boolean = true


scala> def modNCurried(n: Int)(x: Int) = ((x % n) == 0)

modNCurried: (n: Int)(x: Int)Boolean


scala> modNCurried(5)

<console>:13: error: missing argument list for method modNCurried

Unapplied methods are only converted to functions when a function type is expected.

You can make this conversion explicit by writing `modNCurried _` or `modNCurried(_)(_)` instead of `modNCurried`.

       modNCurried(5)


scala> modNCurried(5)(15)

res6: Boolean = true


scala> modNCurried(5)_

res7: Int => Boolean = <function1>


scala> res7(15)

res8: Boolean = true