traverse<L, R, T> static method

Either<L, BuiltList<R>> traverse<L, R, T>(
  1. Iterable<T> values,
  2. Either<L, R> mapper(
    1. T value
    )
)

Traverses the values iterable and runs mapper on each element.

If one of the mapper returns a Left, then it will short-circuit the operation, and returning the first encountered Left.

Otherwise, collects all values and wrap them in a Right.

This is a shorthand for Either.sequence<L, R>(values.map(mapper)).

Example

// Result: Left('3')
Either.traverse<int, String, int>(
  [1, 2, 3, 4, 5, 6],
  (int i) => i < 3 ? i.toString().right() : i.left(),
);

// Result: Right(BuiltList.of(['1', '2', '3', '4', '5', '6']))
Either.traverse<int, String, int>(
  [1, 2, 3, 4, 5, 6],
  (int i) => i.toString().right(),
);

Implementation

static Either<L, BuiltList<R>> traverse<L, R, T>(
  Iterable<T> values,
  Either<L, R> Function(T value) mapper,
) =>
    sequence<L, R>(values.map(mapper));