Tuesday, February 5, 2013

Understanding Currying with Scala

Learning 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) = {
To call this function we would do something like:

(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)
You can see we have created a function that just receives the Message and returns a function that receives the Endpoint and sends the message to that endpoint.

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:

Scala supports a more compact way to defining currying functions. We could replace the route function with the following:

def route(m:Message)(e:Endpoint) = {
The effect is the same if you call like this: route(Message("hi"))(Endpoint("sender")) However to call and store the mid result in a val you need to do it like this:

val routeCiao = route(Message("ciao")) _

Note the underscore at the end of the assignment.


Machiel said...

This isn't called currying but partial application of functions. The trick is that the value that you pass in, in this case Message("ciao"), hangs around after the method is called and sticks to the newly returned method.

Currying is often confused with partial application. http://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application

Good explanation though.

Machiel said...
This comment has been removed by the author.
Unknown said...

I recently read your book-Pro Spring Security.Loved the way of writing, gave me a better understanding of Spring security.
I have a question for you, I read that we can have basic authentication and form based authentication, with Spring-Security-3.2. I have been trying to implement that in my project, in order to secure the REST services.Can you shed some light on that?