Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created March 25, 2012 00:38
Show Gist options
  • Save nakamura-to/2190425 to your computer and use it in GitHub Desktop.
Save nakamura-to/2190425 to your computer and use it in GitHub Desktop.
F# pipeline operator equivalent in JavaScript
function chain(functions) {
return functions.reduceRight(function (next, curr) {
return function () {
var result = curr.apply(null, arguments);
return next.call(null, result);
};
});
}
function pipeline(val) {
return function () {
var functions = Array.prototype.slice.call(arguments);
return chain(functions)(val);
};
}
function add(a, b) {
return a + b;
}
pipeline(10)(add.bind(null, 10), add.bind(null, 1), function (x) { return 10 * x; }, console.log); // 210
// F#
// 10 |> (+) 10 |> (+) 1 |> fun x -> 10 * x |> printfn "%d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment