Skip to content

Instantly share code, notes, and snippets.

@scarletquasar
Created February 22, 2023 02:37
Show Gist options
  • Save scarletquasar/8e3d355dc609b56d7d175964286ba170 to your computer and use it in GitHub Desktop.
Save scarletquasar/8e3d355dc609b56d7d175964286ba170 to your computer and use it in GitHub Desktop.
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