Skip to content

Instantly share code, notes, and snippets.

@evilsoft
Created October 6, 2016 01:40
Show Gist options
  • Save evilsoft/bcde435eac978169060ac1ae818858f1 to your computer and use it in GitHub Desktop.
Save evilsoft/bcde435eac978169060ac1ae818858f1 to your computer and use it in GitHub Desktop.
// 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