Skip to main content
Version: 1.7.0

Higher-order

Functions can take a function as a parameter, or return a function: this is known as higher-order functions. Perhaps the most obvious example is to define a function that takes two functions and returns a function that is their composition, like in mathematics:

const compose = f => g => x => f (g (x));
const double_incr = compose (x => x + 1) (x => 2*x) // 2*x + 1

Of course, we can also pass named functions as arguments:

const increment = x => x + 1;
const double = x => 2*x;
const double_incr2 = compose (increment) (double);