Skip to content

Instantly share code, notes, and snippets.

@kaylarose
Created January 26, 2012 17:27
Show Gist options
  • Save kaylarose/1683912 to your computer and use it in GitHub Desktop.
Save kaylarose/1683912 to your computer and use it in GitHub Desktop.
Function Composition in JS
// Elegant composition
// src: http://news.ycombinator.com/item?id=3512011
function compose(funcA, funcB){
return function(x, result, error){
// Data flows right-to-left over composition, so "do x, then do y"
// is written: "do y" o "do x"
funcB(x, function(x_){ funcA(x_, result, error) }, error);
}
}
cached_lookup = compose(funcTwo, funcOne);
cached_lookup(1234, do_next_step, handle_error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment