Some of the benefits of functional programming.

I just wanted to talk real quick about the benefits of functional programming before going further. I’m not a pro at it, but I really wish I had been introduced to FP much earlier.

If your experience was like mine, you’ve taken a “Intro to OOP” class in University that was either Java or C++. You might have been introduced to recursion at the beginning of class. I did not count much toward your grade, and so you promptly forgot everything about it.

There are whole lot of excellent reasons for programmers to learn it(I hear that it’s a lot easier to do multithreading in it). However we’ll focus solely on the benefits that pertains to us today.

In this case, the best feature are quick iterations and readability.

The best ways to learn is concrete examples. So let’s shamelessly steal a Javascript example

from the Wikipedia article on Functional programming.

Hashtag comments are my own:

const numList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // Count from 1 to 10
let result = 0; //Declare an accumulator 'result' that initializes to zero
for (let i = 0; i < numList.length; i++) { //Take a range of number from 0 until the
  if (numList[i] % 2 === 0) { // If the number at index i of numList is divisible by 2
    result += numList[i] * 10; //Multiply it by 10 and add it to the accumulator 
  }
}7

Functional Programming with higher-order functions:

const result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // Take a list from 1 to 10
               .filter(n => n % 2 === 0) // Retain all even numbers
               .map(a => a * 10) //  Apply the function x*10 to said number
               .reduce((a, b) => a + b); // Add the first and second number //together, then the third to the sum. Rinse, wash, repeat until the end.

Assuming you know what filter,map, and reduce are,you can see that the code is shorter and clearer to read.

There is one important element here: you don’t need to worry about the index of the list (here, the index i). You might think it’s no big deal,but you can see how one might make a typo and end up with an i+1 instead of i+2, perhaps involving indexes i,j,k,l instead of just i. If you want to have nightmares, you can also imagine how such errors might compound in a long program, or a scenario where some indexes start with 0 and others with 1 (Yes, I know, in programming,everything starts with 0, but people IRL do not start by counting with zero so you can be sure there are datasets out there where every index start by 1 and not 0).

You also don’t have to explicitly declare an accumulator.

There are other benefits. For one, functional programming has its roots in math, and thus it is . When you want to translate a formula from a whiteboard to code, you end up with something much more similar than if you were to use loops instead.

There are drawbacks:

#1.OOP is still the dominant paradigm.

One thing that Python doesn’t have is tail call recursion — in practice, that means if you recurse a function more than X amount of time (IIRC, X=2500), the Python interpreter will start complaining very loudly.

#2. In my experience, you have to dig a little bit deeper in the documentation with functional programming whereas in OOP ,it’s easier to bang rocks together since everything is an object.

#3. There are performance hit with recursion , but in the words of Peter Norvig “Premature optimization is the roots of all evils”, we’ll cross that bridge when we get there.

I’m not a purist. Sometime, it’s just shorter and faster to do a quick for loops in particular for very short functions. My policy is to use loops when is convenient (typically for very short/small task) but avoid them if possible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: