partitionEithers<E, A> static method

Tuple2<List<E>, List<A>> partitionEithers<E, A>(
  1. List<Either<E, A>> list
)

Extract all the Left and Right values from a List<Either<E, A>> and return them in two partitioned List inside Tuple2.

Implementation

static Tuple2<List<E>, List<A>> partitionEithers<E, A>(
    List<Either<E, A>> list) {
  final resultListLefts = <E>[];
  final resultListRights = <A>[];
  for (var i = 0; i < list.length; i++) {
    final e = list[i];
    if (e is Left<E, A>) {
      resultListLefts.add(e._value);
    } else if (e is Right<E, A>) {
      resultListRights.add(e._value);
    } else {
      throw Exception(
        "[fpdart]: Error when mapping Either, it should be either Left or Right.",
      );
    }
  }

  return Tuple2(resultListLefts, resultListRights);
}