Function map

  • Map over a Maybe instance: apply the function to the wrapped value if the instance is Just, and return Nothing if the instance is Nothing.

    map works a lot like Array.prototype.map: Maybe and Array are both containers for other things. If you have no items in an array of numbers named foo and call foo.map(x => x + 1), you'll still just have an array with nothing in it. But if you have any items in the array ([2, 3]), and you call foo.map(x => x + 1) on it, you'll get a new array with each of those items inside the array "container" transformed ([3, 4]).

    That's exactly what's happening with map. If the container is empty – the Nothing variant – you just get back an empty container. If the container has something in it – the Just variant – you get back a container with the item inside transformed.

    (So... why not just use an array? The biggest reason is that an array can be any length. With a Maybe, we're capturing the idea of "something or nothing" rather than "0 to n" items. And this lets us implement a whole set of other interfaces, like those in this module.)

    Examples

    const length = (s: string) => s.length;

    const justAString = Maybe.just('string');
    const justTheStringLength = map(length, justAString);
    console.log(justTheStringLength.toString()); // Just(6)

    const notAString = Maybe.nothing<string>();
    const notAStringLength = map(length, notAString);
    console.log(notAStringLength.toString()); // "Nothing"

    Type Parameters

    • T

    • U

    Parameters

    • mapFn: ((t) => U)

      The function to apply the value to if Maybe is Just.

        • (t): U
        • Parameters

          • t: T

          Returns U

    Returns ((maybe) => Maybe<U>)

    A new Maybe with the result of applying mapFn to the value in a Just, or Nothing if maybe is Nothing.

    Typeparam

    T The type of the wrapped value.

    Typeparam

    U The type of the wrapped value of the returned Maybe.

  • Type Parameters

    • T

    • U

    Parameters

    • mapFn: ((t) => U)
        • (t): U
        • Parameters

          • t: T

          Returns U

    • maybe: Maybe<T>

    Returns Maybe<U>

Generated using TypeDoc