Skip to content

Instantly share code, notes, and snippets.

@ddanielbee ddanielbee/maybe.js
Created Mar 8, 2018

Embed
What would you like to do?
Javascript Maybe playground
const Nothing = () => ({
isJust: () => false,
fold: (b, f) => b,
inspect: () => `Nothing`
});
const Just = x => ({
isJust: () => true,
fold: (b, f) => f(x),
inspect: () => `Just${x}`
});
const isJust = x => x.isJust();
const isNothing = x => !x.isJust();
const maybeCatamorph = b => f => x => x.fold(b, f);
const identity = x => x;
const fromMaybe = b => x => x.fold(b, identity);
const listToMaybe = ([head, ...tail]) => (head ? Just(head) : Nothing());
const _ = "";
const maybeToList = x => (isJust(x) ? [fromMaybe(_)(x)] : []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.