Last active
March 16, 2017 12:12
-
-
Save ardeshireshghi/fa046cdea3f66e2a3e600d33825489b4 to your computer and use it in GitHub Desktop.
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 pipelineOld() { | |
var args = [].slice.call(arguments); | |
var fns = (Array.isArray(args[0])) ? args[0] : args; | |
return function() { | |
var memo = arguments[0]; | |
fns.forEach(function(fn) { | |
memo = fn(memo); | |
}); | |
return memo; | |
}; | |
} | |
/** | |
* More than 20% faster than `pipeline` | |
*/ | |
function pipeline(...args) { | |
const fns = (Array.isArray(args[0])) ? args[0] : args; | |
const composedFn = (seed) => { | |
return fns.reduce((result, fn) => fn(result), seed); | |
}; | |
return composedFn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment