Update the rest of Either to match

This commit is contained in:
Eric Rumsey 2025-05-27 12:35:51 -05:00
parent ebc3f07610
commit f432f7796c

View File

@ -3,26 +3,26 @@ export type Right<A> = {readonly _tag: 'Right', readonly right: A}
export type Either<A, E> = Right<A> | Left<E>
// Constructors
export const left = <E>(e: E): Either<E, never> => ({
export const left = <E>(e: E): Either<never, E> => ({
_tag: 'Left',
left: e,
})
export const right = <A>(a: A): Either<never, A> => ({
export const right = <A>(a: A): Either<A, never> => ({
_tag: 'Right',
right: a,
})
// Operations
export const eitherMap =
<E, A, B>(f: (a: A) => B) => (fa: Either<E, A>): Either<E, B> =>
<E, A, B>(f: (a: A) => B) => (fa: Either<A, E>): Either<B, E> =>
fa._tag === 'Right' ? right(f(fa.right)) : fa
export const eitherChain =
<E, A, B>(f: (a: A) => Either<E, B>) =>
(fa: Either<E, A>): Either<E, B> =>
<E, A, B>(f: (a: A) => Either<B, E>) =>
(fa: Either<A, E>): Either<B, E> =>
fa._tag === 'Right' ? f(fa.right) : fa
export const fold =
<E, A, B>(onLeft: (e: E) => B, onRight: (a: A) => B) =>
(fa: Either<E, A>): B =>
<A, B, E>(onLeft: (e: E) => B, onRight: (a: A) => B) =>
(fa: Either<A, E>): B =>
fa._tag === 'Left' ? onLeft(fa.left) : onRight(fa.right)