0

The following Scala code works for me:

def curry(s1: String)(s2: String): String = (s1 + " " + s2).toUpperCase
val uncurry = Function.uncurried(curry _)
println(uncurry("short", "pants"))

However the following code does not:

def cat(s1: String, s2: String): String = (s1 + " " + s2).toUpperCase    
def curry = Function.curried (cat _) 
println(curry("short")("pants"))

The above gives me a compiler error (in Eclipse)

value curried is not a member of object Function

And indeed, intellisense in Eclipse is also missing the curried function on the Function object... Any ideas?

James Crosswell
  • 648
  • 4
  • 13

3 Answers3

5

A curried would take, for example, a function (A, B) => C and turn it into A => B => C. The actual currying and the resulting function depends on the amount of parameters. And currying for only one parameter is not defined, as just results in the same function. So, curried is not defined on the Function object, but on Function2, Function3, and so on. That means, you want to write:

def cat(s1: String, s2: String): String = (s1 + " " + s2).toUpperCase
val curry = (cat _).curried
println(curry("short")("pants"))
knutwalker
  • 5,924
  • 2
  • 22
  • 29
  • Cool, thanks. I'm guessing this must have changed between Scala 2.7 and 2.11 (in the text book I'm reading they use `Function.curried(cat _)`. In any case, I prefer the new syntax :-) – James Crosswell Oct 16 '14 at 21:11
0

It's not on the Function object, but on the Function2Function22 types.

def curry = (cat _).curried
0

It's a method on the function itself

def cat(s1: String, s2: String): String = (s1 + " " + s2).toUpperCase
        //> cat: (s1: String, s2: String)String
def curry = (cat _).curried
        //> curry: => String => (String => String)
println(curry("short")("pants"))
        //> SHORT PANTS
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134