1

I am going through the Scala School basics tutorial at https://twitter.github.io/scala_school/basics.html.

I am trying to understand what the difference between these two definitions is.

Also, if someone could explain currying vs partial application in this context.

def multiply(m: Int)(n: Int): Int = m * n

and

def multiply(m: Int,n: Int): Int = m * n

Sekhon
  • 108
  • 1
  • 10
  • 3
    possible duplicate of [Two ways of currying in Scala; what's the use-case for each?](http://stackoverflow.com/questions/4915027/two-ways-of-currying-in-scala-whats-the-use-case-for-each) – Shadowlands Sep 29 '15 at 03:41

1 Answers1

1

The difference is only in how you actually call this methods. In second case your only choice is to pass both arguments at the same time, like multiply(2,2). In first case you can pass one argument and get function Int => Int and then call it with another argument:

val f: Int => Int = multiply(2) _

f(2) // 4
f(3) // 6
f(525) // 1050

The real power of curried methods is when the second argument is implicit so you don't have to pass it explicitly.

implicit val x = 2
def multiply(m: Int)(implicit n: Int): Int = m * n

multiply(5) //10
ka4eli
  • 5,294
  • 3
  • 23
  • 43
  • Ok. That sort of makes sense now. I can see that. Just trying to connect the dots in my head. How is the first definition different from this `code`def multiply2(a:Int) = { b:Int => a * b }`code` I am new to scala so excuse my being dense. How would you read this in english might help me make sense of it – Sekhon Sep 29 '15 at 06:17
  • @user1299508, see http://stackoverflow.com/questions/4915027/two-ways-of-currying-in-scala-whats-the-use-case-for-each – ka4eli Sep 29 '15 at 07:10