Skip to content

Instantly share code, notes, and snippets.

@jafow
Last active July 28, 2016 20:29
Show Gist options
  • Save jafow/3f840ce2381810025edb8d0c5c94a8b3 to your computer and use it in GitHub Desktop.
Save jafow/3f840ce2381810025edb8d0c5c94a8b3 to your computer and use it in GitHub Desktop.
Combo
/** Write a function `combo` that takes two parameters--fn1, fn2--and returns a function.
* combo should combine the two function parameters and invoke them on any values passed as arguments
* ex:
* var add2 = (n) => n + 2;
* var times3 = (n) => n * 3;
* var plus2AndMultiply3 = combo(add2, times/3)
* plus2AndMulitply3(5) // --> 17
*/
// Function composition in Es6
const combo = (fn1, fn2) => (...args) => fn1(fn2(args));
// ES5
function combo (fn1, fn2) {
return function () {
var args = Array.prototype.slice.call(arguments);
return fn1(fn2(args));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment