Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usefulness (as in practical applications) of Currying v.s. Partial Application in Scala

I'm trying to understand the advantages of currying over partial applications in Scala. Please consider the following code:

  def sum(f: Int => Int) = (a: Int, b: Int) => f(a) + f(b)    def sum2(f: Int => Int, a: Int, b: Int): Int = f(a) + f(b)    def sum3(f: Int => Int)(a: Int, b: Int): Int = f(a) + f(b)      val ho = sum({identity})   val partial = sum2({ identity }, _, _)   val currying = sum3({ identity })    val a = currying(2, 2)   val b = partial(2, 2)   val c = ho(2, 2) 

So, if I can calculate partially applied function that easy, what are the advantages of currying?

like image 968
Hugo Sereno Ferreira Avatar asked Nov 09 '11 10:11

Hugo Sereno Ferreira


People also ask

Why currying is useful in Scala?

Advantages of Currying Function in Scala One benefit is that Scala currying makes creating anonymous functions easier. Scala Currying also makes it easier to pass around a function as a first-class object. You can keep applying parameters when you find them.

What is the difference between partial application and currying?

Simple answer. Currying: Lets you call a function, splitting it in multiple calls, providing one argument per-call. Partial Application: Lets you call a function, splitting it in multiple calls, providing multiple arguments per-call.

What are partially applied functions in Scala?

The Partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e, while invoking a function, we can supply some of the arguments and the left arguments are supplied when required.

What is currying in Scala?

Currying is the process of converting a function with multiple arguments into a sequence of functions that take one argument. Each function returns another function that consumes the following argument.


1 Answers

Currying is mostly used if the second parameter section is a function or a by name parameter. This has two advantages. First, the function argument can then look like a code block enclosed in braces. E.g.

using(new File(name)) { f =>   ... } 

This reads better than the uncurried alternative:

using(new File(name), f => {   ... }) 

Second, and more importantly, type inference can usually figure out the function's parameter type, so it does not have to be given at the call site. For instance, if I define a max function over lists like this:

def max[T](xs: List[T])(compare: (T, T) => Boolean) 

I can call it like this:

max(List(1, -3, 43, 0)) ((x, y) => x < y) 

or even shorter:

max(List(1, -3, 43, 0)) (_ < _) 

If I defined max as an uncurried function, this would not work, I'd have to call it like this:

max(List(1, -3, 43, 0), (x: Int, y: Int) => x < y) 

If the last parameter is not a function or by-name parameter, I would not advise currying. Scala's _ notatation is amost as lightweight, more flexible, and IMO clearer.

like image 198
Martin Odersky Avatar answered Sep 30 '22 16:09

Martin Odersky