As the title says, I want to transform Either<A, B>[]
into Either<A[], B[]>
The rule is, if there is a single left-value (an error), I want to get a left-value with all the errors; otherwise, I want the right answers.
This seems like it should be the easiest thing, but here is all I have:
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);
Seems way too complicated.