Last active
December 21, 2019 00:07
-
-
Save 3v0k4/1a1e7d2319db57018e38d6d57e5cdbba to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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