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
| const Result = require('folktale/result') | |
| const R = require('ramda') | |
| const first = xs => Result.fromNullable(xs[0]) | |
| const parse = s => Result.try(() => JSON.parse(s)) | |
| const upper = s => s.toUpperCase() | |
| const shout = s => s.concat('!') |
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
| const Result = require('folktale/result') | |
| const first = xs => Result.fromNullable(xs[0]) | |
| const parse = s => Result.try(() => JSON.parse(s)) | |
| const upper = s => s.toUpperCase() | |
| const shout = s => s.concat('!') |
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
| const Right = x => | |
| ({ | |
| x, | |
| map: f => Right(f(x)), | |
| inspect: _ => `Right(${x})`, | |
| isLeft: _ => false, | |
| chain: f => f(x), | |
| }) | |
| const Left = x => |
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
| const Right = x => | |
| ({ | |
| x, | |
| map: f => Right(f(x)), | |
| inspect: _ => `Right(${x})`, | |
| isLeft: _ => false, | |
| chain: f => f(x), | |
| }) | |
| const Left = x => |
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
| const parse = s => tryCatch(() => JSON.parse(s)) | |
| const first = xs => fromNullable('empty array', xs[0]) | |
| const upper = s => s.toUpperCase() | |
| const shout = s => s.concat('!') |
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
| const toString = i => String(i) // <-- added this | |
| const functional = s => | |
| Right(s) | |
| .chain(parse) | |
| .chain(first) | |
| .map(toString) // <-- added this | |
| .chain(format) | |
| const functional2 = ss => ss.map(functional) // <-- added this |
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] |
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
| // upper :: String -> String | |
| const upper = s => s.toUpperCase() | |
| // shout :: String -> String | |
| const shout = s => s.concat('!') | |
| // format :: String => Either(String) | |
| const format = s => | |
| Right(s) | |
| .map(upper) |
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
| Right(1).map(x => | |
| Right(2).map(y => x + y)) // Right(Right(3)) | |
| Right(1).chain(x => | |
| Right(2).map(y => x + y)) // Right(3) |
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
| const Right = x => | |
| ({ | |
| x, | |
| map: f => Right(f(x)), | |
| chain: f => f(x) | |
| }) | |
| const Left = x => | |
| ({ | |
| x, |