Demystifying Functional Programming — Higher Order Functions

Shay Tavor
3 min readJan 31, 2019

--

Photo by andrew welch on Unsplash

Functional programming is a huge buzz nowadays and a lot unknowns about it. In this series I’ll try to unlock some of the common and most useful functional programming concepts, and with that I’ll show an interesting and effective JS library — Ramda.js.

Javascript’s ecosystem contains huge number of libraries and platforms that do almost anything you can imagine. One of my favorites is Ramda.js.

Ramda.js is a JS library that gives a set of utility tools for manipulating and handling arrays, collections, objects, strings and more. It’s not that different from similar utilities libraries like Lodash.js and Sugar.js, but Ramda has its specialty — it does it all with functional programming.

Everyone talks about functional programming, and there are a lot of definitions for this paradigm. I think that the most intuitive of them all says that functional programming treats functions as first class citizens, they are the main and most important entities. It also means that we can pass functions as parameters to other functions, or return functions as returned values, which I always found interesting.

A lot of languages have this feature, and that how it’s done in JS:

function execute(arg1, arg2, op) {    return op(arg1, arg2);}

The execute function accepts three parameters. The third of them, op, is a function, and the execute function invokes op on arg1 and arg2 and returns the result.

Now we can use this function like that:

execute(3, 5, (a, b) => a + b);    // 8

Functions that accepts or returns functions are called higher order functions.

Let’s look at an example from Ramda:

The function any accepts a predicate (function) and an array, and returns true if the predicate returns true on at least one of the array’s elements:

R.any( x => x < 0, [1, 2, 3, 4, 5]);    // false

Here we pass the function x => x < 0 that checks if a number is negative. The function any then takes this predicate and invokes it over each of the array’s element. The next example would return true:

R.any(x => x < 0, [1, 2, -4, 5, -2]);    // true

Now let’s try to practice that. Ramda has a function called partition. This function accepts a predicate and a list, and returns a list that contains two lists — in the first one located all the elements from the original list that returned true in the predicate, and in the last are the elements that got false.

Suppose we have a list of users, each user is an object that contains name and an age:

let users = [{name: “John”, age: 17}, {name: “Sara”, age: 25},             {name: “Bob”, age: 30}, {name: “Alice”, age: 16}];

We want to split the list into two lists — the users that are under 18 and those who are above. How would we write it?

It would be very intuitive using the partition function:

let results = R.partition(user => user.age >= 18, users);

Isn’t it nice?

If you liked it and feel curious about Ramda and other cool libraries, check out my new online course, Javascript Greatest Hits.

Up next — functions composition in Ramda.

--

--

Shay Tavor

I’m an academic and freelancer instructor, teaching computer science and software engineering for more than 17 years now.