Functional Programming - Procedural Abstraction: Map, Curry, and Compose¶
1. The Mapping Pattern¶
Abstraction is one of the most powerful tools in programming. But what is it? Let's begin getting our head around this notion by considering the following two examples called addBonusPoint and doubleAll.
This pattern of computation in the preceding example is called the mapping pattern: it takes a function and a list and returns the list obtained by applying the function to each element in the input list. This problem is about the mapping pattern.
2. Function Composition¶
The next example of abstraction we will consider is called function composition. We want a function called compose that takes in two functions f and g and returns the function that first applies g to its argument and then applies f to that result. In other words:
This next problem is about function composition.
3. Currying¶
In the map function we developed earlier:
var map = function (f,ns) {
if (fp.isNull(ns))
return [ ];
else
return fp.cons(
f(fp.hd(ns)),
map(f, fp.tl(ns)));
};
we cannot separate the computations we want to do on list elements, for example, "doubling all of the elements of a list" or "incrementing all of the elements of a list" from their argument list because the map function needs both of its arguments simultaneously.
Instead, we would like to write a function, map1 below, that takes in only a function, for example doubleIt, and returns another function, in our example, the function doubleAll that can be applied in general to all lists of numbers. map1 is a function-creating function whereas map is not.
var map1 = function (f) {
return function (ns) {
if (fp.isNull(ns))
return [ ];
else
return fp.cons(f(fp.hd(ns)), map1(f)(fp.tl(ns)));
};
};
var doubleAll = map1(doubleIt);
doubleAll( [1,2,3,4,5] );
Currying is the process of transforming a function that takes two or more arguments (such as map) into a function (such as map1) that takes the first argument and returns another function that takes in the second argument and returns another function that has the first argument "wired into it" because of the closure that is created by the definition of the outer function. This process is named after renowned logician Haskell Curry.
So our map1 function is a curried version of our map function.
We will abstract this currying pattern by writing a function called curry that curries any two-argument function:
var curry = function (f) {
return function (x) {
return function (y) {
return f(x,y);
};
};
};
Now we no longer need to write map1 but instead can have curry create it for us.
var map1 = curry(map);
As another example of using curry, consider the following fillIn function:
Although the above example may seem a bit contrived, the importance of currying cannot be overstated. It allows us to convert any function of two arguments into a function of one argument that returns a function of one argument. We will return to the importance of doing this in Chapter 3 when we discuss the lambda calculus.
The next problem is about both currying and function composition.
4. More currying¶
The final problem in this module on procedural abstraction will give
you intensive practice with the curry
and compose
functions. This problem is randomized and must be solved three times
in a row.