Function mapOrElse

  • Map over a Maybe instance and get out the value if maybe is a Just, or use a function to construct a default value if maybe is Nothing.

    Examples

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

    const justAString = Maybe.just('string');
    const theStringLength = mapOrElse(getDefault, length, justAString);
    console.log(theStringLength); // 6

    const notAString = Maybe.nothing<string>();
    const notAStringLength = mapOrElse(getDefault, length, notAString)
    console.log(notAStringLength); // 0

    Type Parameters

    • T

    • U

    Parameters

    • orElseFn: (() => U)

      The function to apply if maybe is Nothing.

        • (): U
        • Returns U

    • mapFn: ((t) => U)

      The function to apply to the wrapped value if maybe is Just

        • (t): U
        • Parameters

          • t: T

          Returns U

    • maybe: Maybe<T>

      The Maybe instance to map over.

    Returns U

    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

    • orElseFn: (() => U)
        • (): U
        • Returns U

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

          • t: T

          Returns U

    Returns ((maybe) => U)

      • (maybe): U
      • Parameters

        Returns U

  • Type Parameters

    • T

    • U

    Parameters

    • orElseFn: (() => U)
        • (): U
        • Returns U

    Returns ((mapFn) => ((maybe) => U))

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

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

              • t: T

              Returns U

        Returns ((maybe) => U)

          • (maybe): U
          • Parameters

            Returns U

Generated using TypeDoc