1
0

either.ts 535 B

1234567891011121314151617181920
  1. type Left<T> = { tag: "left", value: T };
  2. type Right<T> = { tag: "right", value: T };
  3. export type Either<L, R> = Left<L> | Right<R>;
  4. export function left<T> (value: T): Left<T> {
  5. return {tag: "left", value: value}
  6. }
  7. export function right<T> (value: T): Right<T> {
  8. return {tag: "right", value: value}
  9. }
  10. export function match<T, L, R> (input: Either<L, R>, left: (l: L) => T, right: (right: R) => T) {
  11. switch(input.tag) {
  12. case "left":
  13. return left(input.value);
  14. case "right":
  15. return right(input.value);
  16. }
  17. }