Created
August 19, 2014 18:59
-
-
Save amacdougall/ad4fa12febbec2b59abc to your computer and use it in GitHub Desktop.
Function composition test question.
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
/** | |
* Given any number of functions, returns a single function which acts as | |
* if each function were called in right to left order. The result of each | |
* function will be the argument to the next. | |
* | |
* To put it another way, compose(f, g)(x) should have the same result | |
* as f(g(x)). | |
* | |
* @param {...Function} functions - Any number of input functions. | |
*/ | |
function compose() { | |
// TODO: implementation. Hint: do not add parameter names. | |
} | |
// TESTS: | |
var double = function(n) { | |
return n * 2; | |
}; | |
var increment = function(n) { | |
return n + 1; | |
}; | |
var f; // our test function | |
f = compose(double); | |
console.log("Expected: %d; actual: %d", 10, f(5)); | |
f = compose(double, double, double); | |
console.log("Expected: %d; actual: %d", 16, f(2)); | |
f = compose(double, increment, increment, increment, increment); | |
console.log("Expected: %d; actual: %d", 2, f(-3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment