Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Either<E, A> = Left<E> | Right<A>;
- interface Left<E> {
- _tag: 'Left',
- value: E
- }
- interface Right<A> {
- _tag: 'Right',
- value: A
- }
- const left = <E, A=never>(e: E): Either<E, A> => ({
- _tag: 'Left',
- value: e
- })
- const right = <A, E=never>(a: A): Either<E, A> => ({
- _tag: 'Right',
- value: a
- })
- function div2ifeven(num: number): Either<string, number> {
- if (num === 0) {
- return left('/0')
- }
- if (num & 1) {
- return left('odd nr')
- }
- return right(2 / num)
- }
- console.log(div2ifeven(2))
- console.log(div2ifeven(0))
- console.log(div2ifeven(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement