Functional programming: concepts and more concepts

f(x)

After knowing what functional programming is, I was wondering how to use it, how do I make it happen and how I need to think in order to do so.  So I dive deep into more concepts and theory before starting to code, or even before starting to design my algorithms in a functional way.

Here I’ll explain some of the concepts and theory I found important, and I say some of them because I’m absolutely sure that there is a few more, because when you say this is it, this is all, I know it it all, “BOOM”, you find out that there’s one more, and after that, one more, and so on.  With all that said lets start:

Purity – Pure functions

This is a quite important concept, without it we will not be able to code in a functional fashion.

Pure functions are functions that receive a parameter or a set of parameters and it only executes calculations on them returning a new value without modifying the parameters given. They are very simple functions in fact.

var t = 10;
function timesTwo(x) {
  return x * 2;
}
var z = timesTwo(t);

Pure functions should take at least one parameter and return something. Pure functions will always return the same value if the same parameter is given and pure functions have no side effects. Yes you read it right, no side effects, no headaches, dizziness or rash. I understand by no side effects to any changes made to elements outside the function, so a pure function cannot alter or modify any variable or data outside of it.

First-class citizens and higher-order functions

Have you ever heard about closures in JavaScript? well … some sort of.

Higher-order functions are a  type of functions that can receive other functions as arguments or return them as a result. Languages that treat functions as first-class citizens (like JavaScript) can handle higher-order functions. In computer science  a first-class citizen in general is an entity that can be passed as an argument, returned from a function, and assigned to a variable, so a higher-order function is a first-class citizen.

// Assigned to a variable
var timesTwo = function(x) {
  return x * 2;
} 

//Passed as argument
function executer(f, x) { //function that takes a function and a value
  return f(x); //returns the execution of that function and value
}

executer(timesTwo, 10);
>20

Immutability

Here is where your mind needs to change (In case you are coming from OOP)

What about if a tell you that the value assigned to a specific variable cannot be altered, and moreover, what if I tell you that you cannot use variables as variables anymore. Well that is pretty much what immutability is about.  Once a variable takes a value, it will always contain that value, like a constant.  Doesn’t look good, right?, but it is more about how you design your program than how you design your data. Immutability has a its benefits and they are greater than the inconvenience it creates.

This is clearly something to think and to talk about.

Recursion

Lovely, isn’t it?

Instead of using for, functional programming uses recursion. By this point all of us should know what recursion is, but since we seem to not use it often I prefer to remind you and my self that recursion is just calling the same function over and over until a condition is met.

function sumUp(start, end) {
    if (start >= end)
        return start;
    return sumUp(start + 1, end)
}

var x = 0;
var y = 10;
sumUp(x, y);
> 10 
console.log(x);
> 0
console.log(y);
>10

Recursion is another important part of maintaining immutability because as you can see in the example on top, there is no changes in the initial values.

Lets see how this keeps moving…


Sources:

  1. Functional Programming: Concepts, Idioms and Philosophy
  2. So You Want to be a Functional Programmer (Part 1)
  3. Functional programming

 

 

Facebook Comments