Skip to content

Instantly share code, notes, and snippets.

@wayneseymour
Forked from tgrecojs/maybe-vs-either.js
Created July 15, 2020 20:17
Show Gist options
  • Save wayneseymour/11001415ff80a6aede1502d5d0731aba to your computer and use it in GitHub Desktop.
Save wayneseymour/11001415ff80a6aede1502d5d0731aba to your computer and use it in GitHub Desktop.
const Maybe = {
Just: value => ({
value,
map: f => Just(f(value)),
toString: () => `Just(${value})`
}),
Nothing: (value = null) => ({
value: null,
map: f => Nothing(null),
toString: () => `Nothing(${value})`
})
}
const Either = {
Right: value => ({
value,
map: f => Right(f(value)),
toString: () => `Right(${value})`
}),
Left: value => ({
value,
map: f => Left(value),
toString: () => `Left(${value})`
}),
}
const{ Just, Nothing } = Maybe;
const { Left, Right } = Either;
/**
*
*/
const maybeTotal = (x) => (x === 'total' ? Just(x) : Nothing());
const eitherTotal = x => x === 'total' ? Right(x) : Left('Input is not valid');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment