Looking to convert an array of type Either<A, B>[]
into Either<A[], B[]>
The goal here is to gather all the left-values (errors) if there is at least one, otherwise return all right answers.
This task may appear straightforward, but my current attempt seems overly complex:
const compress = <A, B>(arr: E.Either<A, B>[]): E.Either<A[], B[]> =>
A.reduce(
E.right([]),
(acc: E.Either<A[], B[]>, v: E.Either<A, B>) =>
E.match(
(a: A) => E.match((aar: A[]) => E.left([...aar, a]),
(bar: B[]) => E.left([a]))(acc),
(b: B) => E.match((aar: A[]) => E.left(aar),
(bar: B[]) => E.right([...bar, b]))(acc)
)(v)
)(arr);
There has to be a simpler way to accomplish this objective.