scala currying by nested functions or by multiple parameter lists

Posted by Morgan Creighton on Stack Overflow See other posts from Stack Overflow or by Morgan Creighton
Published on 2011-01-15T00:53:28Z Indexed on 2011/01/15 3:54 UTC
Read the original article Hit count: 376

Filed under:

In Scala, I can define a function with two parameter lists.

def myAdd(x :Int)(y :Int) = x + y

This makes it easy to define a partially applied function.

val plusFive = myAdd(5) _

But, I can accomplish something similar by defining and returning a nested function.

  def myOtherAdd(x :Int) = {
    def f(y :Int) = x + y
    f _
  }

Cosmetically, I've moved the underscore, but this still feels like currying.

val otherPlusFive = myOtherAdd(5)

What criteria should I use to prefer one approach over the other?

© Stack Overflow or respective owner

Related posts about scala