Function currying and function composition

image

I have been exploring functional programming over the last few weeks, and rather enjoying it. The topic is huge and so far I have only scratched the surface. This post is an explaining of my two favourite concepts so far, currying and composition. Taken individually they are useful but combined can perform so very powerful operations.

All of the examples will be in JavaScript as it is very easy to pass around functions. in fact this is one of the reasons I like JavaScript.

Currying

I have found it best described as a way of incrementally declaring arguments. Currying relies on the fact that functions can return functions. First some code for a function to add two numbers, in what could be considered as the classic way.

var add = function(a, b){

  return a + b;

};

add(3, 5)

=> 8

This code is very simple and appears to have no downsides so why would you change it. The hidden downside is that it requires you to provide both ‘a’ & 'b’ at the same time. Contrast with the curried function below

var add = function(a){

  return function(b){

    return a + b;

  };

};

var add3 = add(3);

add3(5);

In this code first the value of 3 is passed to the add function. This creates a brand new function that will always add 3 to which the value of 5 is passed. This may not seam that revolutionary but can be very useful with operations that are often used. e.g calculating a tax percentage at always 17.5%.

Function composition

Composition is the process of 'piping’ the output of one function 'f’ to the input of a second 'g’ before the first input is present. In the process creating a third function 'h’ which is the equivalent of sequentially applying 'f’ then 'g’.

h(x) = g(f(x)) 

This is all very theoretical and will be better explained by an example. Imagine we want to add 3 to a number and then double the result.

First lets introduce a curried multiply function similar to the curried add function. 

var multiply = function(a){

  return function(b){

    return a * b;

  };

};

Then we create the components of add3 and double

var add3 = add(3);

var double = multiply(2);

We can then pass both of these to our compose function and just use the resulting function.

var operation = compose(double, add3)

operation(1)

=> 8

operation(3)

=> 12

The benefit of this is it allows us to set up very sophisticated operations, all describe in one variable, before any of the data has arrived. The 'dumb’ data is then manipulated in very few steps and intermediate often uninteresting variables are skipped over.

Implementation

Function composition is simple in theory but it implementation can be more tricky, due to complexity in preserving the composed function’s context (what the 'this’ object refers to when executing the function). Solving this requires a good understanding of JavaScripts bind, call and apply. Ideally implementation details would be taken care of and the functionality described above would be available in a Library.

To this end I have been developing ’cumin.js’ a functional utilities library to experiment with these techniques. To build a solid base I have started by reflecting much of the functions available in underscore.js. The single rule is that the data should come last. This is explained in this great talk ’Hey Underscore, your doing it wrong’ from Brian Lonsdorf.

Do take a look at cumin. Its far from finished but it tries to show off this and other powerful functional ideas. In addition to the standard test suite there are also example tests that show more possibilities, such as calculated hypotenuse length in nDimensions and working with country data. There are more coming and welcome contributions could be to have some more examples added.