Skip to content

Instantly share code, notes, and snippets.

@acjr1910
Created August 20, 2020 00:57
Show Gist options
  • Save acjr1910/144ca5f972089f86fa14c1cfa02e022d to your computer and use it in GitHub Desktop.
Save acjr1910/144ca5f972089f86fa14c1cfa02e022d to your computer and use it in GitHub Desktop.
either-monad.js
const Right = x =>
({
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
toString: () => `Right(${x})`
})
const Left = x =>
({
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
toString: () => `Left(${x})`
})
const fromNullable = x =>
x != null ? Right(x) : Left(null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment