Last active
October 6, 2020 04:50
-
-
Save volodymyrprokopyuk/f1d36137766e7dac85c0 to your computer and use it in GitHub Desktop.
Railway Oriented Programming (JavaScript)
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
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