Created
February 22, 2023 02:37
-
-
Save scarletquasar/8e3d355dc609b56d7d175964286ba170 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Either<TLeft, TRight> { | |
protected leftValue: TLeft = null; | |
protected rightValue: TRight = null; | |
static left<T>(value: T) { | |
const either = new Either<T, any>(); | |
either.leftValue = value; | |
return either; | |
} | |
static right<T>(value: T) { | |
const either = new Either<any, T>(); | |
either.leftValue = value; | |
return either; | |
} | |
fold<T>(left: (x: TLeft) => T | void, right: (x: TRight) => T | void) { | |
return !!this.leftValue ? left(this.leftValue) : right(this.rightValue); | |
} | |
} | |
export { Either } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment