Skip to content

Instantly share code, notes, and snippets.

@gabefinch
Last active August 21, 2019 04:20
Show Gist options
  • Save gabefinch/5525011c92661bf74a1b416c573afefa to your computer and use it in GitHub Desktop.
Save gabefinch/5525011c92661bf74a1b416c573afefa to your computer and use it in GitHub Desktop.
// Identity functor
const Box = x => ({
map: f => Box(f(x)),
fold: f => f(x),
inspect: () => `Box(${x})`
});
// const Either = Right || Left
const Right = x => ({
chain: f => f(x),
map: f => Right(f(x)),
fold: (f, g) => g(x),
inspect: () => `Right(${x})`
});
const Left = x => ({
chain: f => Left(x),
map: f => Left(x),
fold: (f, g) => f(x),
inspect: () => `Left(${x})`
});
const fromNullable = x => x != null ? Right(x) : Left(x));
const tryCatch = f => {
try {
return Right(f());
}
catch(e) {
return Left(e);
}
}
// Semigroups
const All = x => ({
x,
concat: ({ x: y }) => All(x && y),
inspect: () => `All(${x})`;
});
const First = x => ({
x,
concat: _ => First(x),
inspect: () => `First(${x})`
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment