-
-
Save JonathanDCohen/79fab45340616347a199 to your computer and use it in GitHub Desktop.
category-2.1-3
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
//only works for single argument functions | |
function compose(g, f) { return function (x) { return g(f(x))} ; } | |
function id(x) { return x; } | |
function add_one(x) { return x + 1; } | |
function times_two(x) { return x * 2; } | |
var two_x_plus_1 = compose(add_one, times_two); | |
console.log(two_x_plus_1(1)); //3 | |
console.log(two_x_plus_1(3)); //7 | |
console.log(two_x_plus_1(-4)); //-7 | |
console.log(compose(two_x_plus_1, id)(1)); //3 | |
console.log(compose(two_x_plus_1, id)(3)); //7 | |
console.log(compose(two_x_plus_1, id)(-4));//-7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment