Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
category-2.1-3
//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
You can’t perform that action at this time.