Skip to content

Instantly share code, notes, and snippets.

@evilsoft
Last active February 11, 2017 00:00
Show Gist options
  • Save evilsoft/e0ca1a88d4b96cd753fcd3a6cf2c4c95 to your computer and use it in GitHub Desktop.
Save evilsoft/e0ca1a88d4b96cd753fcd3a6cf2c4c95 to your computer and use it in GitHub Desktop.
const {
Either, coalesce, compose,
constant, identity, ifElse,
isString, map
} = require('crocks')
const { Left, Right } = Either
// onlyStrings : a -> Either Number String
const onlyStrings =
ifElse(isString, Right, constant(Left(0)))
// shout : String -> String
const shout =
str => str.toUpperCase()
// flow : a -> Either _ String
const flow = compose(
coalesce(constant('[default]'), identity),
map(shout),
onlyStrings
)
console.log(flow(3)) // Right "[default]"
console.log(flow('three')) // Right "THREE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment