Skip to content

Instantly share code, notes, and snippets.

@daronwolff
Created May 20, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daronwolff/3a3b199be90cc3f28b2e546dd5e1348e to your computer and use it in GitHub Desktop.
Save daronwolff/3a3b199be90cc3f28b2e546dd5e1348e to your computer and use it in GitHub Desktop.
Write a TWICE, function that takes a binary function and returns a unitary function that passes its arguments to the binary function twice Example var double = twice(add); double(11); // 22 var square = twice(mul); square(11); // 121
var add = (function(a) {
return function(b) {
return a + b;
}
});
var mul = (function(a) {
return function(b) {
var total = a * b;
return total;
}
});
function twice(func){
return function(d){
return func(d)(d);
}
}
var double = twice(add);
console.log(double(11)); // 22
var square = twice(mul);
console.log(square(11)); // 121
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment