JSFiddle - React, Tailwind, and code Playground
by dp0ch
JavaScript
// MAYBE
//INTERFACE:
// bind - monadic bind
// toString - obvious
// isSome - whether the monads constructor was Just - also () => !maybe.isNone()
// isNone - whether the monads constructor was Nothing - also () => !maybe.isSome()
// some - supplies the wrapped value (will throw on Nothing)
// orSome - supplies the wrapped value or arg0 (e.g. default) if Nothing
// oElse - returns the monad if it's some - otherwise it returns the Maybe from arg0
const Just = (a) => {
const monad = {
bind: (f) => f(a),
toString: () => `Just(${a})`,
isSome: () => true,
isNone: () => false,
some: () => a,
orSome: () => a,
orElse: () => monad,
};
return monad;
}
const Nothing = {
bind: (f) => Nothing,
toString: () => 'Nothing',
isSome: () => false,
isNone: () => true,
some: () => a,
orSome: () => a,
orElse: () => Nothing,
};
export {
Just,
Nothing,
};