Skip to content
Blog Snippets About

Map


Run the given function over each element of an array in a composable way.

This is really just a wrapper for javascripts inbuilt Array.map function, so why is it useful?

It all comes down to function composition. This style of mapping is able to be combined with the pipe function in order to create more complex functionality.

Lets say you have an input file which is a list of numbers. There are two common styles that can be used to parse this to an array of ints in js:

  • Imperative
  • Functional

The choice is mostly down to preference. Below shows both styles doing the same parsing.

const input = `123`;

// Imperative
const chars = input.split('');
const asInts = chars.map((v) => parseInt(v));

// Functional
const splitToChars = v => v.split('');
const parseToInt = v => parseInt(v);
const parse = pipe(
  splitToChars,
  map(parseToInt)
);
const result = parse(input);

Both of these end up with the same result, but the functional one creates small modular building blocks that are then combined. This can be really useful, but again is really a preference.