Development/Programming

[Scala] Currying

Gomp 2015. 11. 23. 16:27

간단하게 설명하면 여러 개의 인자를 받는 함수에 일부 인자값을 넣어서 다시 함수로 만드는 것이다. (참고사이트)


  1. object CurryTest extends App {
  2. def filter(xs: List[Int], p: Int => Boolean): List[Int] =
  3. if (xs.isEmpty) xs
  4. else if (p(xs.head)) xs.head :: filter(xs.tail, p)
  5. else filter(xs.tail, p)
  6. def modN(n: Int)(x: Int) = ((x % n) == 0)
  7. val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
  8. println(filter(nums, modN(2)))
  9. println(filter(nums, modN(3)))
  10. }


modN의 함수를 Currying 함


  1. List(2,4,6,8)
  2. List(3,6)