Forked from volodymyrprokopyuk/railway_oriented_programming.js
Created
March 9, 2017 12:40
-
-
Save kaosat-dev/e95e13e73eb053e69e0b1736d8d7368d to your computer and use it in GitHub Desktop.
Railway Oriented Programming (JavaScript)
This file contains 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
var _ = require('lodash'); | |
var Success = function(success) { this.success = success; }; | |
var Failure = function(failure) { this.failure = failure; }; | |
var bindAll = function(fs) { | |
var bind = function(res, f) { | |
return res instanceof Success ? f(res.success) : res; | |
}; | |
var bindF = function(f) { return _.partial(bind, _, f); }; | |
return _.flow.apply(_, _.map(fs, bindF)); | |
}; | |
var f1 = function(s) { // input => Success | Failure | |
return new Success(s + ' f1'); | |
return new Failure('error f1'); | |
}; | |
var f2 = function(s) { | |
return new Success(s + ' f2'); | |
return new Failure('error f2'); | |
}; | |
var f3 = function(s) { | |
return new Success(s + ' f3'); | |
return new Failure('error f3'); | |
}; | |
console.log(bindAll([ f1, f2, f3 ])(new Success('start'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment