DEV Community

Iven Marquardt
Iven Marquardt

Posted on

Sorry, but Functors are not a means to map over an Array

...because they are much more general: Functors lift any ordinary function into numerous structures and effectful contexts.

See how we lift the pure function inc = x => x + 1 in a context that either produces an Array asynchronously or yields nothing at all. All it takes is the composition of these three functions:

const tMap = f => tx =>
  Task((res, rej) => tx.task(x => res(f(x)), rej));

const optMap = f => tx =>
  match(tx, {
    None: _ => None,
    Some: ({some: x}) => Some(f(x))
  });

const arrMap = f => xs =>
  xs.map((x, i) => f(x, i));

Consider what it means in terms of code reuse in general if you can reuse all your simple functions in dozens of scenarios.

Read the full story about functors including the complete, runnable code of the given example.

Top comments (0)