Development/Programming
[Scala] Currying
Gomp
2015. 11. 23. 16:27
간단하게 설명하면 여러 개의 인자를 받는 함수에 일부 인자값을 넣어서 다시 함수로 만드는 것이다. (참고사이트)
object CurryTest extends App {
def filter(xs: List[Int], p: Int => Boolean): List[Int] =
if (xs.isEmpty) xs
else if (p(xs.head)) xs.head :: filter(xs.tail, p)
else filter(xs.tail, p)
def modN(n: Int)(x: Int) = ((x % n) == 0)
val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
println(filter(nums, modN(2)))
println(filter(nums, modN(3)))
}
modN의 함수를 Currying 함
List(2,4,6,8)
List(3,6)