Skip to content

Instantly share code, notes, and snippets.

@volodymyrprokopyuk
Last active October 6, 2020 04:50
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save volodymyrprokopyuk/f1d36137766e7dac85c0 to your computer and use it in GitHub Desktop.
Save volodymyrprokopyuk/f1d36137766e7dac85c0 to your computer and use it in GitHub Desktop.
Railway Oriented Programming (JavaScript)
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