As a pasionate software developer, motivated by learning and appliyng innovative and interesting software development tools, techniques and methodologies, my professional objectives are the following. To be in a technology oriented enterprise where the technichal staff is the soul of the company. To be in an important IT team. Be able to design and develop state of the art software. Be able to apply new knowledge everyday, on innovative ways and with a great degree of freedom. To architect, design and develop software that uses the best practices of the field. Play with the latest technologies, learn everyday and participate in the research and innovation of the software products. Carlo is a DZone MVB and is not an employee of DZone and has posted 16 posts at DZone. You can read more from them at their website. View Full User Profile

Understanding Currying with Scala

02.16.2013
| 8064 views |
  • submit to reddit

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) = {
  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)
}
 
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:

route(Message("hi"))(Endpoint("sender"))
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) = {
   e.send(m)
}
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.



Published at DZone with permission of Carlo Scarioni, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)