Created
May 20, 2016 22:31
-
-
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
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
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