separate<A, B> function
- ImmutableList<
Either< eithersA, B> >
Separates a list of either.Either values into two lists: one containing all the either.Left values and the other containing all the either.Right values.
Given an ImmutableList of either.Eithers, this function will produce a record containing two ImmutableLists: one for values that are either.Left and another for values that are either.Right.
Examples:
final eithers = ImmutableList([
Left<int, String>(1),
Right<int, String>('a'),
Left<int, String>(2),
Right<int, String>('b'),
]);
final result = separate(eithers);
print(result.lefts); // Outputs: ImmutableList([1, 2])
print(result.rights); // Outputs: ImmutableList(['a', 'b'])
When provided with an empty list:
final emptyEithers = ImmutableList<either.Either<int, String>>([]);
final emptyResult = separate(emptyEithers);
print(emptyResult.lefts); // Outputs: ImmutableList([])
print(emptyResult.rights); // Outputs: ImmutableList([])
@param eithers An ImmutableList of either.Either values to be separated.
@return A record with two properties: lefts
and rights
which are ImmutableLists containing the separated values.
Implementation
({ImmutableList<A> lefts, ImmutableList<B> rights}) separate<A, B>(
ImmutableList<either.Either<A, B>> eithers) =>
foldLeft<
either.Either<A, B>,
({
ImmutableList<A> lefts,
ImmutableList<B> rights
})>((lefts: zero<A>(), rights: zero<B>()))((acc, curr) {
if (curr is either.Left<A, B>) {
return (lefts: append(curr.value)(acc.lefts), rights: acc.rights);
} else if (curr is either.Right<A, B>) {
return (lefts: acc.lefts, rights: append(curr.value)(acc.rights));
}
return acc;
})(eithers);