Last active
August 29, 2015 14:02
-
-
Save bttmly/64036c109e96422327ba to your computer and use it in GitHub Desktop.
Curry and compose for functional programming.
This file contains hidden or 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
| // 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