Skip to content

Instantly share code, notes, and snippets.

@ddanielbee
Created March 8, 2018 15:49
Show Gist options
  • Save ddanielbee/0ef3c0f124719fda1045dd7edd5c3ddf to your computer and use it in GitHub Desktop.
Save ddanielbee/0ef3c0f124719fda1045dd7edd5c3ddf to your computer and use it in GitHub Desktop.
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