Thoughts

I’m writing this more as a way to document and organize my thoughts around the list monad.

This is a very important monad.

A map of functional concepts I’m currently studying can be found at Functional Programming Concepts.

The Code

export const List = list => ({
  list,
  map: fn => List(list.map(fn)),
  fold: (fn, initialValue) => List(list.reduce(fn, initialValue)),
  foldMap(monoid, empty) {
    const mappedList = list.reduce(fn, initialValue);
    const result = mappedList.reduce(
      (accumalator, monoidValue) => accumalator.concat(monoidValue),
      empty,
    );
    return List(result);
  },
  extract: () => list,
});

Example

Here is a sample implementation of this list monad.

List([1, 2, 3, 4]).map(x => x + 1)
//> [2,3,4,5]

List(["red", "Blue", "Yellow", "Green"]).(str => str.toUpperCase())
//> ["RED", "BLUE", "YELLOW", "GREEN"]

Github

A current version of my implementation of this list monad can be found here.