Skip to content

Instantly share code, notes, and snippets.

@3v0k4
Last active December 21, 2019 00:07
Show Gist options
  • Select an option

  • Save 3v0k4/1a1e7d2319db57018e38d6d57e5cdbba to your computer and use it in GitHub Desktop.

Select an option

Save 3v0k4/1a1e7d2319db57018e38d6d57e5cdbba to your computer and use it in GitHub Desktop.
// tryCatch :: Function => Either(String)
const tryCatch = f => {
try {
return Right(f())
} catch (e) {
return Left('could not parse')
}
}
// parse :: String -> Either [String]
const parse = s => tryCatch(() => JSON.parse(s))
// fromNullable :: String -> a -> Either(a)
const fromNullable = error => a =>
(a === undefined || a === null) ? Left(error) : Right(a)
// first :: [a] -> Either(a)
const first = xs => fromNullable('empty array')(xs[0])
// format :: String => Either(String)
const format = s => Right(s).map(upper).map(shout)
// functional :: String -> Either(String)
const functional = s => Right(s).chain(parse).chain(first).chain(format)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment