I'm trying to learn Scala and I'm confused when to use currying functions over partially applied functions.
I'm pretty sure those concepts were not made to be redundant, but I can't see the real purpose of the different approaches.
I was experimenting with this code:
Solution uses currying:
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)))
}
Solution uses partially applied function:
object PartialFunctionTest 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,_)))
}
Both give the same result:
List(2, 4, 6, 8)
List(3, 6)
Are those different approaches equivalent?
Could somebody enlighten me, what is the best use case for each?