How we can define and invoke a curried function in scala?

I heard about curried function in scala, how we can use it and what's its benefit..Explain with proper examples..

Comment actions Permalink

There are two real benefits.
The first one is block syntax:

def withResource(closable: Closable)(body: => Unit) = {
  try {
  } finally {

withResource(someResource) {

So you will get "new language syntax".

The second one is type inference:

def foldLeft[B](z: B)(op: (B, A) => B): B
def wrongFoldLeft[B](z: B, op: (B, A) => B): B
foldLeft("")((x, y) => "") //it's ok not to put parameter types here for x and y as compiler can infer
wrongFoldLeft("", (x: String, y: Int) => "") //You cannot omit parameter types here as compiler can't infr them

You can also ask questions about Scala in Scala user group (on google groups). You can get answer much faster.

Best regards,
Alexander Podkhalyuzin.

Comment actions Permalink

The purpose of function currying is to easily get specialized functions from more general functions. You achieve this by pre-setting some some parameters at a different time and keeping them fixed afterwards. It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.
def simple_function(a):
    def line(b=0):
        def compute(x):
            return [a + b * xi for xi in x]
        return compute
    return line

x = range(-4, 4, 1)
print('x {}'.format(list(x)))
print('constant {}'.format(simple_function(3)()(x)))
print('line {}'.format(simple_function(3)(-2)(x)))                  


x [-4, -3, -2, -1, 0, 1, 2, 3]
constant [3, 3, 3, 3, 3, 3, 3, 3]
line [11, 9, 7, 5, 3, 1, -1, -3]

Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c) with calls of type f(a)(b)(c) which might even be seen as the less elegant style in Python.

But it allows you to do:

line_through_zero = simple_function(0)
print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x
which gives

line through zero [-4, -3, -2, -1, 0, 1, 2, 3]
So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.

Alternatives to currying are: partial, lambda and default parameters. So in practice currying might be useful but you can also get around it if you want.

Comment actions Permalink

Currying means taking a function that takes multiple arguments and turning it into a chain of functions each taking one argument and returning the next function, until the last returns the result.

scala> def curriedSum(x: Int)(y: Int) = x + y

curriedSum: (Int)(Int)Int

scala> curriedSum(1)(2)

res5: Int = 3

A way to write functions with multiple parameter lists. For instance def f(x: Int)(y: Int) is a curried function with two parameter lists.A curried function is applied by passing several arguments lists, as in: f(3)(4). However, it is also possible to write a partial application of a curried function, such as f(3).A curried function is applied to multiple argument lists, instead of just one. Currying is like a kind of incremental binding of function arguments. More about Scala for beginners to advance level


Please sign in to leave a comment.