The Functional Approach

Today I would like to talk about one of my favourite topics, programming theory. Do not be alarmed, however, I’m not about to bore you with mundane details. Over the past few years discussion about functional programming has become quite common, though a lot of confusion remains. There are many reasons that contribute to the problem, but mostly its because many main stream languages aren’t known for their functional properties.

So, what are the real benefits in this approach and why should you care? Here are just some of the benefits I have learned of…

The real benefit is that functional programming is declarative. What does that mean to you as a programmer? The answer to that question lies in a small mind shift. Instead of focusing on how something is to be done, you shift into focusing on what that something is.  A small example:

// traditionally, when working on a sequence you would write loops:  var sequence = [1, 2, 3];  for (index in sequence) {     alert(sequence[index]); }  // using a list comprehension we can capture the concept of // using alert on each item of the list without explicitly // setting up a looping construct.  [ alert(item) for each(item in sequence)]

What we’ve done here is put the computation right into the identity of the list. What you see between the [ and ] *is* the list where each item is expressed as a formula that is applied to each item of ’sequence’ (another list). Now, lets take this a step further:

var sequence = [1, 2, 3, 4];  [item * item for each (item in sequence) if item % 2 == 0].map(function(item) { alert(item); });

In one single line, we’ve singled out all the even numbers of ’sequence’, squared them, and applied an arbitrary piece of code to each of those values. Here we’re the code simply pops an alert box with the value on to the screen.

This style, with a bit of practice, can greatly reduce complexity by localizing the code that traditionally had to be written in separate locations, even though the different bits of code themselves do not have much meaning in isolation. Consider a more traditional procedural implementation of the line above, first we define the function that will execute for each element. Notice how it is a separate definition to itself:

// here we pretend that this function will never be used // anywhere except for one specific set of values // contained in ‘sequence’.  function alert_item(item) { alert (item); }

Then we have to define the looping construct which will select every item (even and odd):

for (index in sequence) {     // we can now filter based on even / odd,     // notice that you have to manually extract the value out of the sequence using index     if (sequence[index] % 2 == 0) {         // finally we can apply the code we want to to each of the filtered elements (evens).         var item = sequence[index] * sequence[index];         alert_item(item);     } }

At each step both the programmer writing the code and the programmer reading the code become painfully aware of ‘how’ the computer will be doing something. What that something is is essentially lost in the noise of all the different constructs. In this trivial example it is still possible to quickly grasp what that something is, however, as the code size grows, the number of functions to maintain also grows (usually to a much larger number than anyone would like). We as human beings will always have a limit of what is manageable at any given instance, and the more we can do to limit what we absolutely have to remember in each given instance, the better.

The point of this exercise was to illustrate the disconnect that emerges from doing very simple, everyday things, and to show, that one flexible way of dealing with this problem is to localize sections of code by utilizing functional programming techniques. Today we’ve seen the application of filters by singling out specific elements of one list as items of a new list. Then we transformed each of those items to its square. There is one more trick that is indispensable to functional programming: Reducing a list to a single value. That is a topic that deserves its own post devoted to it.

This is where I will end this post. I hope that you found it useful and entertaining. Functional programming is a very large topic, and I’ve only barely scratched the surface. Though with the few little pieces presented here one can already begin taking advantage of these rather powerful techniques.

– Written by: Ivgeni Slabkovski, Programmer

PrintFriendly
EmailTwitterFacebookMySpaceLinkedInDeliciousGoogle BookmarksYahoo BookmarksStumbleUponAIMShare
http://blogs.citydirect.info/developing-web-apps/?p=41#comment-