const Maybe = {
Nothing: x => {
return x.Nothing;
},
Just: a => x => {
return x.Just(a);
}
};
const map = (f, x) => x(
{ Just: a => Maybe.Just(f(a))
, Nothing: Maybe.Nothing
}
)
const flatMap = (f, x) => x(
{ Just: f
, Nothing: Maybe.Nothing
}
)
const divide = (x, y) =>
y === 0
? Maybe.Nothing
: Maybe.Just(x / y)
[ Maybe.Nothing;
, flatMap(x => divide(x, 2), map(x => x + 5, Maybe.Just(5)))
, Maybe.Just("hi")
, Maybe.Just(true)
].forEach(example => {
// example of how to "pattern match" on these vals
const result = example({
Nothing: "there was nothing here",
Just: a => "there was something here: " + a
});
console.log(result);
});