Created
October 6, 2016 01:40
-
-
Save evilsoft/bcde435eac978169060ac1ae818858f1 to your computer and use it in GitHub Desktop.
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
// A common use for chaining Maybes is deep pulls on objects | |
// where there is uncertainty | |
const { curry, Maybe, option } = crocks | |
const data = { | |
happy: { joy: true } | |
} | |
// propMaybe : String -> Object -> Maybe | |
const propMaybe = curry( | |
(prop, x) => Maybe(x[prop]) | |
) | |
// pathMaybe : [ String ] -> Object -> Maybe | |
const pathMaybe = curry( | |
(props, x) => props.reduce( | |
(m, prop) => m.chain(propMaybe(prop)), | |
Maybe(x) | |
) | |
) | |
const path = [ 'happy', 'joy' ] | |
const nope = [ 'hapi', 'joi' ] | |
console.log(pathMaybe(path, data).option(false)) // valid returns true | |
console.log(pathMaybe(nope, data).option(false)) // not valid returns false (because of option) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment