Last active
April 14, 2016 14:14
-
-
Save DrBoolean/8357229 to your computer and use it in GitHub Desktop.
Pointfree fantasy working with folktale's either
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
require("pointfree-fantasy").expose(global); | |
var curry = require("lodash.curry"); | |
var fs = require("fs"); | |
var Either = require("data.either"); | |
// HELPERS | |
// ============ | |
var add = curry(function(x,y) { return x + y }); | |
var exists = function(path) { | |
return fs.existsSync(path); | |
}; | |
var fread = function(path) { | |
return fs.readFileSync(path, 'utf-8'); | |
}; | |
// ACTUAL EXAMPLE | |
// =============== | |
// Returns the contents of the file at `path`, if it exists. | |
// | |
// read : String -> Either(Error, String) | |
function read(path) { | |
return exists(path) ? Either.Right(fread(path)) : Either.Left("Non-existing file: " + path); | |
} | |
var intro = read('intro.txt') // => Right(...) | |
var outro = read('outro.txt') // => Right(...) | |
var nope = read('nope.txt') // => Left("Non-existing file: nope.txt") | |
var r = liftA2(add, intro, outro) | |
console.log('r', r); // => Right(...) | |
var r1 = liftA2(add, intro, nope) | |
console.log('r1', r1); // => Left("Non-existing file: nope.txt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment