6

I wanted to clarify benefits about currying in scala. According to "Programming in Scala Second Edition" - "currying 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)." "c"

One benefit connected with creation partially applied functions like this

def multiplyCurried(x: Int)(y: Int) = x * y 
def multiply2 = multiplyCurried(2) _

But we also can use partially applied functions without currying

def multiplyCurried(x: Int,y: Int) 
def multiply2 = multiplyCurried(2, _) 

Could you please give a few example, where currying will show benefits ?

DNA
  • 42,007
  • 12
  • 107
  • 146
initmax
  • 395
  • 2
  • 10
  • See also https://stackoverflow.com/questions/12660852/why-does-currying-in-scala-need-multiple-parameter-lists?rq=1 – DNA Dec 25 '14 at 14:58
  • 4
    possible duplicate of [What's the difference between multiple parameters lists and multiple parameters per list in Scala?](http://stackoverflow.com/questions/6803211/whats-the-difference-between-multiple-parameters-lists-and-multiple-parameters) In particular see [this answer](http://stackoverflow.com/a/6803909/699224) which discusses benefits. – DNA Dec 25 '14 at 14:59

2 Answers2

1

Curry functions become really useful when you have implicit parameters.

Take for example the map function on futures:

map[S](f: (T) ⇒ S)(implicit executor: ExecutionContext): Future[S]

The second parameter is implicit, and this definition allows you to write expressions like

val f2 = f1.map(x => x)

when you have the implicit value in scope.

Another place where curry is useful is a common pattern in the Scala code where you would like to pass a function to a method (it may be a callback function, for instance).

Take for example the "loan" pattern for input streams, that allow you to use a stream without thinking about proper closing the resource

def withInputStream[T](opener: => InputStream)(f: InputStream => T): T = ...

withInputStream(new InputStream("hello.txt")) { inputStream =>
  readLines(inputStream)
}

This kind of syntax makes the code clearer in a lot of cases

Marius Danila
  • 10,311
  • 2
  • 34
  • 37
0

From Coursera course:

Assume you have these functions:

def sum(f:Int=>Int, a:Int, b:Int) = f(a) + f(b)
def sumInts(a:Int,b:Int)=sum(x=>x,a,b)
def sumCubes(a:Int,b:Int)=sum(x=>x*x*x,a,b)

Change sum into a function that returns another function:

def sum(f:Int=>Int):(Int,Int)=>Int={
   def sumF(a:Int,b:Int):Int= 
      if(a>b) 0 else f(a)+ sumF(a+1,b)
   sumF
  }

now we can define the functions we had in following way:

def sumInts=sum(x=>x)
def sumCubes=sum(x=>x*x*x)

Now we can use it in simple way:

sumInt(10,11) + sumCubes(3,4)

Now we have these sumInt and sumCubes that we want to avoid them:

sum(cube)(3,4)

so currying coming to help to not write nested function in functional programming, let redefine sum:

def sum(f:Int=>Int)(a:Int,b:Int):Int=
  if(a>b) 0 else f(a)+sum(f)(a+1,b)
Omid
  • 1,959
  • 25
  • 42
  • 1
    Thanks for your answer, but we can do the same things with high order function without currying if I`m not mistaken. – initmax Dec 26 '14 at 13:22