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:
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:
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:
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:
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:
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:
Note the underscore at the end of the assignment.