Understanding Currying with Scala
Join the DZone community and get the full member experience.
Join For FreeLearning Scala is not an easy task. There are many things to learn. Some
of them are very different and some of them seem complex.
Currying is a technique not necessarily complex, but is one that you
probably are not familiar with if you come from Java background as it is
not a straightforward technique to apply in that language. Currying
allows to turn a function that expects two arguments into a function
that expects only one, and that function returns a function that expects
the second argument. Creating basically a chain of functions.
Let’s see with a simplistic example how it works.
Let’s say we have this two simple classes:
case class Message(value: String){ } case class Endpoint(prompt: String){ def send(m: Message) { println(this.prompt + " " + m.value) } }
Now let’s pretend we want to send a Message to an Endpoint in a general function. We can create a function that looks like this:
def routeTo(m:Message, e:Endpoint) = { e.send(m) }
To call this function we would do something like:
routeTo(Message("hola"),Endpoint("sending"))
(You could ask why we just don’t create the endpoint and call send with
the message like Endpoint(“sending”).send(Message(“hola”)) and there is
no reason in this example. Simply to illustrate currying).
Now let’s suppose we want to send the same message to different
endpoints. For that we could use the currying technique. We will
basically turn our function into a function that returns another
function:
def route(m:Message) = { (e: Endpoint) => e.send(m) }
Now we could do something like the following:
val routeCiao = route(Message("ciao")) routeCiao(Endpoint("sending again")) routeCiao(Endpoint("sending over again"))You can see that we are assigning the return of route(Message(“ciao”)), which is a function (Endpoint => Unit) to the val routeCiao. In practice this is basically returning the function (e: Endpoint) => e.send(“ciao”) as the value of the variable m is replaced by “ciao” when calling to the route function.
Then we are simply calling the returned function passing the value of the endpoint as the only argument. So this function is executed and the message is sent to the endpoint.
You could of course call the currying function without storing the mid result in a val. Like this:
route(Message("hi"))(Endpoint("sender"))
def route(m:Message)(e:Endpoint) = { e.send(m) }
val routeCiao = route(Message("ciao")) _
Note the underscore at the end of the assignment.
Published at DZone with permission of Carlo Scarioni, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments