Skip to content

Instantly share code, notes, and snippets.

@bttmly
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save bttmly/64036c109e96422327ba to your computer and use it in GitHub Desktop.

Select an option

Save bttmly/64036c109e96422327ba to your computer and use it in GitHub Desktop.
Curry and compose for functional programming.
// adapted from:
// http://javascriptweblog.wordpress.com/2010/04/05/curry-cooking-up-tastier-functions/
// http://javascriptweblog.wordpress.com/2010/04/14/compose-functions-as-building-blocks/
function curry( fn ) {
if ( arguments.length < 2 ) {
return fn;
}
var args = [].slice.call( arguments, 1 );
return function() {
return fn.apply( args.concat( [].slice.call( arguments ) );
}
}
// like this h(x) = f(g(x))
function compose( fFn, gFn ) {
return function() {
var args = [].slice.call( arguments );
return fFn.call( this, gFn.apply( this, args ) );
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment