Eskilop.it

It’s been quite some time now that the computer science community gained interest into this particular programming paradigm. To most of the people who write and talk about it, seems a really big thing, but why is it?

Functional programming, as the name implies, is all about functions. Pure functions are some kind of object which given a certain input, provide a certain output and for the same input, they give the same output.

We can see that this kind of paradigm, removes one of the most important points of failure in most algorithms: relying on other data to make some computation.

In fact, a function described like this, doesn’t make use of any data outside of itself to fulfil its purpose, neither changes any of it.

This may not sound important yet, but from this simple concept, you can derive some interesting properties.

Real-life example

Let’s see how we can use a functional approach to do a very common operation, like modifying the contents of an array. So, let’s say that you want a function which, given an array of integers, returns the same exact array but with all the integers incremented by one. First thing that may come to your mind may be similar to this:

function sum_one_to_array(array) {
    for (var i = 0; i < array.length; i++) {
        a[i]+=1;
    }
}

This is a typical approach to transform some values inside an array, though it doesn’t really follow along with our definition of ‘functional’ because it modifies the data. So what can we do about it?

We can start by defining an actual function, to do the sum for us:

function sum_one(i) {
    return i + 1;
}

And we can notice, this is a pure function, though a very simple one, in fact, it does not modify any data, and given a certain input, it produces the same output every time.

We can use it, to build a new array, so we can solve the data modification problem:

function sum_one_to_array(array) {
    var result = [];
    for (var i = 0; i < array.length; i++) {
        result.push(sum_one(a[i]));
    }
    return result;
}

The next step

This is already a functional approach, though if, say we wanted a function which multiplies 2 to the elements of the array, we would have to rewrite the entire function, for such a small change.

In this situation, functional programming and recursion come in handy and help us to write less code.

If you look closely, you may notice that all the functions which modify the elements of an array, are all made up in a similar fashion:

function sum_one_to_array(array) {
    // we start with an empty array
    var result = [];
    // we iterate through the original
    for (var i = 0; i < array.length; i++) {
        // we 'add' the transformed element to the array
        result.push(sum_one(a[i]));
    }
    // we provide the result
    return result;
}

The only thing that changes, is which function we decide to apply to that element. So we can take another step of abstraction here, and think of such a function, as a combination of iteration and function application:

function map(func, arr) {
    if (arr.length === 0)
        return [];
    else
        return [func(arr[0])].concat(
            map(func, arr.slice(1, arr.length))
        );
}

What we have just done, is a function, but it doesn’t feel like just a normal function, does it?

In fact, it’s called a Higher-order function, as wikipedia puts it:

In mathematics and computer science, a higher-order function is a function that does at least one of the following:

• takes one or more functions as arguments (i.e. procedural parameters),

• returns a function as its result.

Functional programming has a lot of other interesting properties and a lot of other uses. But that’s out of the scope of this post. Do you like functional programming? Do you have doubts or requests? Discuss in the group

Tags: