First-class and Higher Order Functions: Effective Functional JavaScript
Last updated
Last updated
JavaScript is a very in-demand language today. It runs in a lot of places from the browser to embedded systems and it brings a non-blocking I/O model that is faster than others for some types of applications. What really sets JavaScript apart from the rest of scripting languages is its highly functional nature.
JavaScript has more in common with functional languages like Lisp or Scheme than with C or Java. — Douglas Crockford in JavaScript: the World’s Most Misunderstood Language
It’s a widely publicised fact that Brendan Eich, when creating the first version of JavaScript, was actually told he would be able to build a Lisp, a list processing language like Scheme or common Lisp. It turns out that Sun Microsystems had other ideas and this programming language needed to wear a coat of procedural syntax inherited from a C or Java-like language. Despite all the semicolons, at its heart JavaScript remains a functional language with features such as first-class functions and closures.
This post will focus on first-class functions and higher order functions. To read more about closures and how to leverage them to wield JavaScript’s functional powers,
A language with first-class functions means that it treats functions like expressions of any other type. Functions are like any other object.
You can pass them into other functions as parameters:
You can assign a function as a value to a variable:
You can return a function:
In languages without first-class functions to pass a dependency in you usually inject an object. Here we can just pass functions around, which is great.
For example in Java to run a custom sort on an ArrayList, we have to use ArrayList#sort
which expects a Comparator
object as a parameter (see the Java API docs here). In JavaScript we use Array#sort
(MDN reference here) which expects a function. This is a bit less verbose since in this case we’re just going to be implementing one method of the Comparator
interface
It gets even better with ES6 default parameter syntax.
JavaScript’s default behaviour on IO is non-blocking. This means we tend to pass a lot of callbacks (until Promises came along at least). Being able to pass a function as a callback instead of an object on which we will run a method (like we would in say Java) means we can have terseness in callback-based code.
For example in Node:
Being able to return a function and closures means we have access to things like partial application and currying.
It also means we can start creating higher order functions.
A function is a higher order function if it takes a function as a parameter, or returns a function as its result. Both of these requirements rely on functions being first-class objects in a language.
map
, filter
and reduce
/reduceRight
are the functions present in JavaScript that map to classic higher order functions in other functional languages.
In other languages:
map tends to be called map or transform
filter is called select in some languages
reduce and reduceRight are the fold left and right functions (also called accumulate, aggregate, compress or inject in different languages)
In functional programming languages, there are no loops. When you need to do an operation like traversing a list or a tree, there are two predominant styles: recursion and higher order functions.
Recursion relies on a function calling itself, usually on a different part of the parameter (list being traversed for example). That’s a topic for another post
Higher order functions are usually provided by the language. In ES6 JavaScript these functions are defined on the Array prototype.
map
is used if we want to perform the same change on each member of the array. It takes the function that should be applied to each element of the array as a parameter. That function is passed (element, index, wholeArray)
as parameters.
filter
allows us to pick which elements of the array should remain in the transformed list by passing a filtering function that returns a Boolean value (true/false). As for map
this functions is passed (element, index, wholeArray)
.
reduce
is used to change the shape of the array. We provide more than 1 parameter to this function (in addition to the array we’re reducing). We pass a reducing function and optionally the initial value of to reduce with. The function is passed (prev, curr, index, wholeArray)
. prev
is the value returned by the previous reduction, for the first iteration that means it’s either the initial value of the first element of the array. curr
is the value in the array at which we’re at.
The classic example is summing or concatenating.
If you want to learn more about the internal of map
, filter
and reduce
or recursion, I’ve reimplemented them in a recursive style using ES6 destructuring in the following post:
Higher order functions allow us to get rid of the imperative loops that seem to be spread everywhere.
The intent is just clearer with map. We can also extract the double
function so we’re making our code more readable and modular.
It reads like a book and that’s important because we write for humans not machines.
Array
higher order functions do not mutate the variable they are called on. This is good, because the loop-based approach using .push
and .pop
changes it. It means if you pass a variable as a parameter, it’s not suddenly going to get changed by a function down the call stack.
In some languages functions like map
are parallelised. That’s because we don’t actually need to know what’s in the rest of the array to compute this particular element’s new value. If we’re doing complex things in the mapping function then this sort of optimisation could be very useful.
Use first-class and higher order functions to write nicer code more easily. It’s nice and declarative instead of imperative, say what you want done not how to do it
This will enable you to compose your functions and write code that is extremely terse without losing readability.