Pergunta

I'm new to scala, and just playing around with some code. I created a curried function from an example I found online line this:

    def adder(a: Int, b: Int) = a + b

    var addto = (adder _ ).curried

and it works. But, when I try replacing adder with an anonymous function like..

    var addto = ({(a :Int, b: Int) => a + b} _ ).curried

I get an error saying:

    error: _ must follow method; cannot follow (Int, Int) => Int

any idea why this doesn't work?

Foi útil?

Solução

You dont need placeholder (_)

scala> var addto = ({(a :Int, b: Int) => a + b}  ).curried
addto: Int => (Int => Int) = <function1>

scala> addto(1)
res0: Int => Int = <function1>

scala> res0(2)
res1: Int = 3

This is because you already have a function object with you here on which you can call curried.

Where as in your previous case

var addto = (adder _ ).curried

You first had to convert the method to a function object (by using placeholder) before doing curried on it.

Outras dicas

Try this

 var addto = ((a :Int, b: Int) => a + b ).curried
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top