Functional programming (FP) aims to create predictable code by eliminating so-called side effects, bringing with it confidence that the same code with the same arguments will produce the same results. In order to achieve this, developers rely on higher-order functions: functions that operate on other functions.
forEach() takes an array, iterates over its values and applies the specified function on them. It can be considered an abstracted version of the basic
for loop: we don’t define a counter, check the length of the array, tell it how to increment (or decrement, for that matter), nor have to define a new variable within the loop body to store the current element.
While the applied function can be created on the spot, we can also provide a function that has been written earlier in the hopes of adhering to the principle of DRY.
forEach() (and the functions mentioned below) were ‘only’ standardised in 2009, so aren’t available in older browsers. Should you rather not write a polyfill yourself, lodash (or Underscore) provide an interface to the same functionality:
Interestingly, Ben McCormick tells us that the two libraries are both faster than their native counterparts in this case:
A little research indicates that this is because native functions optimize for sparse arrays and have more weird corner cases that they handle. In any case, the performance difference is pretty startling across the board.
While the native implementation is currently only available on arrays, lodash’s implementation is designed to operate on any kind of collection, including object literals and NodeLists.
map() method takes an array, iterates over its contents, applies a function to all of its elements, and returns a new array with the transformed values.
This helps streamline the process of avoiding ‘side effects’ - the function isn’t altering our original array, so it can be used again without mishap.
When we want to get just a single value from an array, we can turn to
reduce(). This method aggregates all values in a collection and returns a single value from them: whether that be a miniumum, maximum, average, or what-have-you.
reduce() takes a transformative function, and an intial value it should start computing with:
The functions featured above are useful additions to a developer’s toolkit. That these are higher-order functions means that they allow developers to write code that is simple to comprehend. There is no mess of code dealing with initialising loops to wade through. As these functions work on the output of other functions, they can be chained together quite trivially. Here’s lodash’s example for its
Straightforward, self-documenting code.