fold<L, R, T> function

T Function(Either<L, R> either) fold<L, R, T>(
  1. T ifLeft(
    1. L left
    ),
  2. T ifRight(
    1. R right
    )
)

Transforms an Either using the ifLeft and ifRight functions.

expect(
  right(1).chain(fold(
    (_) => -1,
    (number) => number + 1,
  )),
  equals(2),
);
expect(
  left('fail').chain(fold(
    (left) => 'caught: $left',
    (right) => 'yay!',
  )),
  equals('caught: fail'),
);

Implementation

T Function(Either<L, R> either) fold<L, R, T>(
  T Function(L left) ifLeft,
  T Function(R right) ifRight,
) =>
    (either) => either._fold(ifLeft, ifRight);