|
| 1 | +/* tslint:disable:prefer-function-over-method */ |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by tushar on 07/09/19 |
| 5 | + */ |
| 6 | + |
| 7 | +export abstract class Either<L1, R1> { |
| 8 | + public static left<L>(left: L): Either<L, never> { |
| 9 | + return new Left(left) |
| 10 | + } |
| 11 | + public static neither(): Either<never, never> { |
| 12 | + return new Neither() |
| 13 | + } |
| 14 | + public static right<R>(right: R): Either<never, R> { |
| 15 | + return new Right(right) |
| 16 | + } |
| 17 | + |
| 18 | + public abstract biChain<L2, R2>( |
| 19 | + LL: (l: L1) => Either<L2, R2>, |
| 20 | + RR: (r: R1) => Either<L2, R2> |
| 21 | + ): Either<L2, R2> |
| 22 | + |
| 23 | + public biMap<L2, R2>(LL: (l: L1) => L2, RR: (r: R1) => R2): Either<L2, R2> { |
| 24 | + return this.mapL(LL).mapR(RR) |
| 25 | + } |
| 26 | + |
| 27 | + public chain<R2>(ab: (r: R1) => Either<L1, R2>): Either<L1, R2> { |
| 28 | + return this.chainR(ab) |
| 29 | + } |
| 30 | + public chainL<L2>(ab: (l: L1) => Either<L2, R1>): Either<L2, R1> { |
| 31 | + return this.biChain(ab, Either.right) |
| 32 | + } |
| 33 | + public chainR<R2>(ab: (r: R1) => Either<L1, R2>): Either<L1, R2> { |
| 34 | + return this.biChain(Either.left, ab) |
| 35 | + } |
| 36 | + |
| 37 | + public abstract fold<S>( |
| 38 | + seed: S, |
| 39 | + LL: (l: L1, s: S) => S, |
| 40 | + RR: (r: R1, s: S) => S |
| 41 | + ): S |
| 42 | + |
| 43 | + public abstract getLeftOrElse(left: L1): L1 |
| 44 | + public abstract getRightOrElse(right: R1): R1 |
| 45 | + public map<R2>(ab: (r: R1) => R2): Either<L1, R2> { |
| 46 | + return this.mapR(ab) |
| 47 | + } |
| 48 | + public mapL<L2>(ab: (r: L1) => L2): Either<L2, R1> { |
| 49 | + return this.chainL(r => Either.left(ab(r))) |
| 50 | + } |
| 51 | + |
| 52 | + public mapR<R2>(ab: (r: R1) => R2): Either<L1, R2> { |
| 53 | + return this.chainR(r => Either.right(ab(r))) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class Left<L1> extends Either<L1, never> { |
| 58 | + public readonly isLeft = true |
| 59 | + public readonly isNeither = false |
| 60 | + public readonly isRight = false |
| 61 | + |
| 62 | + public constructor(public readonly left: L1) { |
| 63 | + super() |
| 64 | + } |
| 65 | + |
| 66 | + public biChain<L2, R2>( |
| 67 | + LL: (l: L1) => Either<L2, R2>, |
| 68 | + RR: (r: never) => Either<L2, R2> |
| 69 | + ): Either<L2, R2> { |
| 70 | + return LL(this.left) |
| 71 | + } |
| 72 | + |
| 73 | + public fold<S>(S: S, LL: (l: L1, s: S) => S, RR: (r: never, s: S) => S): S { |
| 74 | + return LL(this.left, S) |
| 75 | + } |
| 76 | + |
| 77 | + public getLeftOrElse(left: L1): L1 { |
| 78 | + return this.left |
| 79 | + } |
| 80 | + |
| 81 | + public getRightOrElse(right: never): never { |
| 82 | + return right |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class Right<R1> extends Either<never, R1> { |
| 87 | + public readonly isLeft = false |
| 88 | + public readonly isNeither = false |
| 89 | + public readonly isRight = true |
| 90 | + |
| 91 | + public constructor(public readonly right: R1) { |
| 92 | + super() |
| 93 | + } |
| 94 | + |
| 95 | + public biChain<L2, R2>( |
| 96 | + LL: (l: never) => Either<L2, R2>, |
| 97 | + RR: (r: R1) => Either<L2, R2> |
| 98 | + ): Either<L2, R2> { |
| 99 | + return RR(this.right) |
| 100 | + } |
| 101 | + public fold<S>(S: S, LL: (l: never, s: S) => S, RR: (r: R1, s: S) => S): S { |
| 102 | + return RR(this.right, S) |
| 103 | + } |
| 104 | + |
| 105 | + public getLeftOrElse(left: never): never { |
| 106 | + return left |
| 107 | + } |
| 108 | + public getRightOrElse(right: R1): R1 { |
| 109 | + return this.right |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +class Neither extends Either<never, never> { |
| 114 | + public readonly isLeft = false |
| 115 | + public readonly isNeither = true |
| 116 | + public readonly isRight = false |
| 117 | + |
| 118 | + public biChain<L2, R2>( |
| 119 | + LL: (l: never) => Either<L2, R2>, |
| 120 | + RR: (r: never) => Either<L2, R2> |
| 121 | + ): Either<L2, R2> { |
| 122 | + return this |
| 123 | + } |
| 124 | + |
| 125 | + public fold<S>( |
| 126 | + S: S, |
| 127 | + LL: (l: never, s: S) => S, |
| 128 | + RR: (r: never, s: S) => S |
| 129 | + ): S { |
| 130 | + return S |
| 131 | + } |
| 132 | + |
| 133 | + public getLeftOrElse(left: never): never { |
| 134 | + return left |
| 135 | + } |
| 136 | + |
| 137 | + public getRightOrElse(right: never): never { |
| 138 | + return right |
| 139 | + } |
| 140 | +} |
0 commit comments