Function transposeArray

  • Given an array or tuple of Maybes, return a Maybe of the array or tuple values.

    • Given an array of type Array<Maybe<A> | Maybe<B>>, the resulting type is Maybe<Array<A | B>>.
    • Given a tuple of type [Maybe<A>, Maybe<B>], the resulting type is Maybe<[A, B]>.

    If any of the items in the array or tuple are Nothing, the whole result is Nothing. If all items in the array or tuple are Just, the whole result is Just.

    Examples

    Given an array with a mix of Maybe types in it, both allJust and mixed here will have the type Maybe<Array<string | number>>, but will be Just and Nothing respectively.

    import Maybe, { transposeArray } from 'true-myth/maybe';

    let valid = [Maybe.just(2), Maybe.just('three')];
    let allJust = transposeArray(valid); // => Just([2, 'three']);

    let invalid = [Maybe.just(2), Maybe.nothing<string>()];
    let mixed = transposeArray(invalid); // => Nothing

    When working with a tuple type, the structure of the tuple is preserved. Here, for example, result has the type Maybe<[string, number]> and will be Nothing:

    import Maybe, { transposeArray } from 'true-myth/maybe';

    type Tuple = [Maybe<string>, Maybe<number>];

    let invalid: Tuple = [Maybe.just('wat'), Maybe.nothing()];
    let result = transposeArray(invalid); // => Nothing

    If all of the items in the tuple are Just, the result is Just wrapping the tuple of the values of the items. Here, for example, result again has the type Maybe<[string, number]> and will be Just(['hey', 12]:

    import Maybe, { transposeArray } from 'true-myth/maybe';

    type Tuple = [Maybe<string>, Maybe<number>];

    let valid: Tuple = [Maybe.just('hey'), Maybe.just(12)];
    let result = transposeArray(valid); // => Just(['hey', 12])

    Note: this does not work with ReadonlyArray. If you have a ReadonlyArray you wish to operate on, you must cast it to Array insetad. This cast is always safe here, because Array is a wider type than ReadonlyArray.

    Type Parameters

    • T extends Maybe<unknown>[]

    Parameters

    • maybes: T

      The Maybes to resolve to a single Maybe.

    Returns TransposedArray<T>

Generated using TypeDoc