Skip to content

Instantly share code, notes, and snippets.

@JonathanDCohen
Created December 16, 2014 18:39
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 JonathanDCohen/79fab45340616347a199 to your computer and use it in GitHub Desktop.
Save JonathanDCohen/79fab45340616347a199 to your computer and use it in GitHub Desktop.
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