Configuring function call

In programming, everywhere in the code we use procedural abstractions in the form of function/procedure or method (in object-oriented programming).  These procedural abstractions (later in the article I will call them just functions) usually have parameters. It is interesting that if you fix one or more parameters to some particular value, the function can acquire additional meaning and can be used as a dedicated function with a separate name.  For example, consider function add(x,y).  When you take the first parameter and fix it to some number, for example, x=1, then this function can acquire new meaning – incrementing. Function add(1, y) now can be named increment(y). The example is a bit unpractical, but this is done for the sake of simplicity. Although developers don’t pay much attention to such situations much, they often have to make design decision: what parameter to fix and what should be the name of the new function. After this decision is made, there are several techniques which can be used to implement this decision.

The first one is classical: using the object or just plain parameter passing. (in object-oriented programming sense). When the object is constructed, the value to be fixed is passed into the constructor. Here is C# implementation of incrementing functionality based on addition function:

In some sense, it is not the most elegant solution because Of method actually takes increment parameter implicitly. It could be done even in C by explicit passing increment parameter to Of function: Of(increment,5). It is not elegant because 1) caller should always know about increment parameter and apply it to Of function; 2) new semantics is conveyed through parameter name, that is “increment” which is the name of the parameter, not function. The function itself has a unexpressive name.

Much more interesting way to do the same thing is using closure. The closure is basically a function that has a special ability to access other variables local to the scope it was created in. Here is implementation of increment using closures in C# (yes, this great language actually support closures!):

If syntaxis of Evaluator method looks weird to you, it’s just C#6 feature – expression-bodied function. In this snippet, Evaluator takes argument x and then it looks like evaluation function gets called with an x parameter. But it is not what happens. In fact whole function y => evaluate(x, y) returns as a result and x is “closed” in this function, that is when Evaluator function returns, x variable is not destroyed as it normally would, it continues its life incide y => evaluate(x, y). That is why increment(5) gives 6. The parameter x still lives in object referenced by increment variable. Sounds cool? Not as cool as the next method!

Even more interesting, terse and elegant way to implement increment is using currying. Currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example evaluation of function, add(x,y) can be considered as two steps process. 1) The parameter x is applied to add which yields another function, which takes one argument. The value of x is “embedded” inside returned function in the same way as it was done with closures. 2) Parameter y is applied to the just returned function which yields the sum of x and y. Unfortunately, C# does not support currying and hardly ever will. Therefore here is implementation of increment with currying in F#:

So, everything you do is “let increment = add 1”! It is hard to imagine something even more expressive than that.

It is not that currying is impossible to do in C# at all. You still can do some ugly hacks, the question, of course, is whether you want to do it. But who knows, maybe one day you will need it:

JavaScript is an interesting language. It does not support much stuff, but it is so flexible and dynamic, that one can introduce lacking concepts into the language using libraries. There is a very nice library, named Underscore.js, which facilitates very much functional programming in JavaScript. See how it is relatively easy to do currying in JavaScript using Underscore.js (actually I demonstrate partial function application here, but the subtle difference between partial function application and currying is off topic):

If it is hard to you to wrap your head around all these concepts, check out some course on functional programming. Sooner or later in your career you will encounter them again.
I can recommend a very nice introductory course from Delft university on edX.


Leave a Reply

Your email address will not be published.