Created
March 25, 2012 00:38
-
-
Save nakamura-to/2190425 to your computer and use it in GitHub Desktop.
F# pipeline operator equivalent in 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
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