Functional Programming in JavaScript

JavaScript offers several programming constructs that support functional programming. Some of them are filter, map and reduce and can all be found as part of the Array object.

Here's some code that demonstrates filter, map and reduce:

Functional Programming Code Example Part 1

So, line 1 creates an object named sdt (short for Some Dev Thoughts) which is used as a namespace. Lines 3-24 adds a function named "contains" to the Array prototype. This makes the "contains" function available to any and all Arrays.

Functional Programming Code Example Part 2

Lines 26-159 create an array containing cryptocurrency data. This data was found on Wikipedia and Coin Market Cap.

Functional Programming Code Example Part 3

Lines 164-194 define a number of pure functions that are used later on in the program. Pure functions are defined as follows:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).

The above definition came from Wikipedia.

Functional Programming Code Example Part 4

Lines 196-221 contain the functional operations in the example.

Line 198 creates a variable named cc which aliases the cryptocurrency data. It's easier to reference cc instead of sdt.cryptoCurrencies. Line 199 creates a variable named funcs which aliases the pure functions we defined under sdt.funcs.

Lines 201 and 202 simply output the number of cryptocurrencies we have in our data.

Lines 204 and 205 output the number of active cryptocurrencies we have. This is done by using the filter function passing it funcs.isActive. Function isActive returns true or false indicating whether a cryptocurrency is active or not.

Lines 207 and 208 output the number of cryptocurrencies that use POS timestamping. This one uses the filter function passing it funcs.usesPosTimestamping. Function usesPosTimestamping returns true or false indicating whether a cryptocurrency is defined as using POS timestamping.

Lines 210 to 212 output the total market capitalization for all of the cryptocurrencies. On line 210, the map function is passed funcs.getMarketCap to transform each cryptocurrency object which contains several properties into a single value containing the MarketCap property. Function funcs.getMarketCap simply returns the value of MarketCap. The reduce function is then executed for each MarketCap value calling funcs.add for each one. Function funcs.add simply adds two numbers together returning their sum. Line 211 formats the result of summing all of the market cap values using the library accounting.js.

The accounting.js project can be found on GitHub here.

Lines 214 to 217 determine how many cryptocurrencies in our data are using the SHA-256d hashing algorithm. This is done by executing the filter function passing it an anonymous function. This anonymous function operates as a predicate returning true or false indicating whether the hashing algorithm is SHA-256d or not.

Here is the resulting output:

Resulting Output

Our queries found a total of 12 cryptocurrencies. 10 of them are active. 3 of them are using POS timestamping. The computed total market capitalization was $5,116,433,963.00. Finally, it found 2 cryptocurrencies using SHA-256d hashing.

Functional programming constructs are quite useful when operating on sets of data. The functions filter, map and reduce can be used in combination. Finally, these functional constructs can be provided a variety of user-defined functions to achieve the desired result.