sequenceList<E, A> function
- ImmutableList<
Either< listE, A> >
Takes an ImmutableList
of Either
values and attempts to extract their right-hand values.
This function iterates through each Either
value in the input list.
If any of them is a Left
, the function immediately returns that Left
value.
If all are Right
values, it collects them into an ImmutableList
and wraps it in a Right
.
Parameters:
list
: AnImmutableList
ofEither
values to be processed.
Returns:
An Either
containing an ImmutableList
of the right-hand values.
Returns the first Left
encountered while processing the input list.
Examples:
var listOfEithers = il.ImmutableList<Either<String, int>>([
Right(1),
Right(2),
Right(3)
]);
var result = sequenceList(listOfEithers);
print(result); // Outputs: Right(ImmutableList([1, 2, 3]))
var listOfEithersWithLeft = il.ImmutableList<Either<String, int>>([
Right(1),
Left("Error"),
Right(3)
]);
var resultWithError = sequenceList(listOfEithersWithLeft);
print(resultWithError); // Outputs: Left("Error")
Implementation
Either<E, il.ImmutableList<A>> sequenceList<E, A>(
il.ImmutableList<Either<E, A>> list) {
final result = <A>[];
for (var e in list) {
switch (e) {
case Left(value: var leftValue):
return Left<E, il.ImmutableList<A>>(leftValue);
case Right(value: var rightValue):
result.add(rightValue);
}
}
return Right(il.of(result));
}