Skip to content

Instantly share code, notes, and snippets.

@mofas
Created December 26, 2016 01:53
Show Gist options
  • Select an option

  • Save mofas/83a05267e255528cd4e5bfe7ac8edebd to your computer and use it in GitHub Desktop.

Select an option

Save mofas/83a05267e255528cd4e5bfe7ac8edebd to your computer and use it in GitHub Desktop.
DrBoolean FP constructure
// from https://github.com/DrBoolean/Practically-Functional
const Box = x =>
({
chain: f => f(x),
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
})
const Right = x =>
({
chain: f => f(x),
ap: other => other.map(x),
traverse: (of, f) => f(x).map(Right),
map: f => Right(f(x)),
fold: (f, g) => g(x),
inspect: () => `Right(${x})`
})
const Left = x =>
({
chain: f => Left(x),
ap: other => Left(x),
traverse: (of, f) => of(Left(x)),
map: f => Left(x),
fold: (f, g) => f(x),
inspect: () => `Left(${x})`
})
const fromNullable = x =>
x != null ? Right(x) : Left(null)
const tryCatch = f => {
try {
return Right(f())
} catch(e) {
return Left(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment