Compare commits

..

3 Commits

2 changed files with 9 additions and 9 deletions

View File

@ -7,7 +7,7 @@
"quoteStyle": "alwaysSingle",
"trailingCommas": "onlyMultiLine",
"operatorPosition": "nextLine",
"arrowFunction.useParentheses": "preferNone",
"arrowFunction.useParentheses": "maintain",
"binaryExpression.linePerExpression": false,
"memberExpression.linePerExpression": false,
"bracePosition": "sameLineUnlessHanging",

View File

@ -1,28 +1,28 @@
export type Left<E> = {readonly _tag: 'Left', readonly left: E}
export type Right<A> = {readonly _tag: 'Right', readonly right: A}
export type Either<E, A> = Left<E> | 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)