Created
April 13, 2020 15:02
-
-
Save YBogomolov/24127fe7b287608a9ee7bb431f0d0bfc to your computer and use it in GitHub Desktop.
Encoding Either<E, A> as a tuple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export type Either<E, A> = [E, null] | [null, A]; | |
type Fn<A, B> = (a: A) => B; | |
export const left = <E, A>(e: E): Either<E, A> => [e, null]; | |
export const right = <E, A>(a: A): Either<E, A> => [null, a]; | |
export const isLeft = <E, A>(e: Either<E, A>): e is [E, null] => e[1] === null; | |
export const isRight = <E, A>(e: Either<E, A>): e is [null, A] => e[0] === null; | |
/** | |
* Functor, bifunctor, monad, alternative, applicative instances, and `fold` to unwrap an Either: | |
*/ | |
export const either = { | |
map: <E, A, B>(e: Either<E, A>, f: Fn<A, B>): Either<E, B> => (isRight(e) ? [null, f(e[1])] : e), | |
mapLeft: <E, E1, A>(e: Either<E, A>, f: Fn<E, E1>): Either<E1, A> => (isLeft(e) ? [f(e[0]), null] : e), | |
bimap: <E, E1, A, B>(e: Either<E, A>, f: Fn<E, E1>, g: Fn<A, B>): Either<E1, B> => (isRight(e) ? [null, g(e[1])] : [f(e[0]), null]), | |
of: right, | |
ap: <E, A, B>(e: Either<E, A>, ef: Either<E, Fn<A, B>>): Either<E, B> => either.chain(ef, f => either.map(e, f)), | |
chain: <E, A, B>(e: Either<E, A>, f: Fn<A, Either<E, B>>): Either<E, B> => (isRight(e) ? f(e[1]) : e), | |
alt: <E, A>(e: Either<E, A>, fallback: Either<E, A>): Either<E, A> => (isRight(e) ? e : fallback), | |
fold: <E, A, B>(e: Either<E, A>, onLeft: Fn<E, B>, onRight: Fn<A, B>): B => (isRight(e) ? onRight(e[1]) : onLeft(e[0])) | |
}; | |
export const collectRight = <E, A>(es: Array<Either<E, A>>): Either<E, A[]> => es.reduce( | |
(acc, curr) => either.chain(curr, a => either.map(acc, as => as.concat(a))), | |
either.of<E, A[]>([]) | |
); | |
export const collectAll = <E, A>(es: Array<Either<E, A>>): Either<E[], A[]> => es.reduce( | |
(acc, curr) => either.bimap( | |
acc, | |
lefts => (isLeft(curr) ? lefts.concat(curr[0]) : lefts), | |
rights => (isRight(curr) ? rights.concat(curr[1]) : rights) | |
), | |
either.of<E[], A[]>([]) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've also written a curried instances for sake of pointfree usage: