Created
January 4, 2018 01:34
-
-
Save desmondrawls/05b86aba803e7359befada7c772c3c9f to your computer and use it in GitHub Desktop.
This file contains 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
interface Either<V> { | |
map: <T>(f: (V) => T) => Either<T> | |
fold: <L,R>(l: (V) => L, r: (V) => R) => L | R | |
} | |
function left<V>(x: V): Either<V> { | |
return { | |
map: _ => left(x), | |
fold: (l, r) => l(x) | |
} | |
} | |
function right<V>(x: V): Either<V> { | |
return { | |
map: <T>(f: (V) => T) => right<T>(f(x)), | |
fold: (l, r) => r(x) | |
} | |
} | |
let rightFour: Either<Number> = right(4) | |
let rightFive = rightFour.map(x => x + 1) | |
let five = rightFive.fold(l => l, r => r) | |
let result = document.createElement('h1'); | |
result.textContent = five.toString(); | |
document.body.appendChild(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment