• #">Summary #
  • #">Who Cares? #

    FP defines its own types, and functors are among the most basic. Think of it as a container that holds any value. It can be a string, date, boolean, array, object, etc.
    This container must have a map method to be considered a functor.
    Here’s a basic example

    1. const Identity = (x) => ({
    2. value: x,
    3. map: (fn) => Identity(fn(x))
    4. });

    This function, Identity, takes a value and returns a functor.

    1. const name = Identity('Bobo');
    2. console.log(name);

    Logging name shows us a functor of this shape

    1. { value: 'Bobo', map: [Function: map] }

    How would you change this functor’s inner value, ‘Bobo’?
    This is JavaScript, so we could mutate it.

    1. const name = Identity('Bobo');
    2. name.value = name.value.toUpperCase();
    3. console.log(name);

    But we know FP actively discourages this practice, so what if we used map instead?

    1. const name = Identity('Bobo');
    2. const newName = name
    3. .map(value => value.toUpperCase());
    4. console.log({ name, newName });

    This way is immutable, easier to read, and composes better.
    And when you need to extract it, just access its .value property.

    Summary #

    • Functors are containers you can map over.
    • Functors can hold any value
    • map is a composable, immutable way to change a functor’s value.

      Who Cares? #

      Lenses use functors. We’ll be diving into them very soon.