Function matchEither

  • Takes two functions and an Either value, if the value is a Left the inner value is applied to the first function, if the value is a Right the inner value is applied to the second function.

    Type Parameters

    • E
    • A
    • B

    Parameters

    • onLeft: ((e) => B)
        • (e): B
        • Parameters

          Returns B

    • onRight: ((a) => B)
        • (a): B
        • Parameters

          Returns B

    Returns ((ma) => B)

      • (ma): B
      • Parameters

        • ma: Either<E, A>

        Returns B

    Example

    import { match, left, right } from 'fp-ts/Either'
    import { pipe } from 'fp-ts/function'

    function onLeft(errors: Array<string>): string {
    return `Errors: ${errors.join(', ')}`
    }

    function onRight(value: number): string {
    return `Ok: ${value}`
    }

    assert.strictEqual(
    pipe(
    right(1),
    match(onLeft, onRight)
    ),
    'Ok: 1'
    )
    assert.strictEqual(
    pipe(
    left(['error 1', 'error 2']),
    match(onLeft, onRight)
    ),
    'Errors: error 1, error 2'
    )

    Since

    2.10.0