Skip to content

Instantly share code, notes, and snippets.

@desmondrawls
Created January 4, 2018 01:34
Show Gist options
  • Save desmondrawls/05b86aba803e7359befada7c772c3c9f to your computer and use it in GitHub Desktop.
Save desmondrawls/05b86aba803e7359befada7c772c3c9f to your computer and use it in GitHub Desktop.
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