Created
March 8, 2018 15:49
-
-
Save ddanielbee/0ef3c0f124719fda1045dd7edd5c3ddf to your computer and use it in GitHub Desktop.
Javascript Maybe playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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