Forked from cowboy/is-this-partial-application.js
Created
September 16, 2010 20:43
-
-
Save Kambfhase/583131 to your computer and use it in GitHub Desktop.
This file contains 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
// So what I'm doing here seems cool, but I'm not sure exactly what it's called. | |
// (also, I'm not doing anything smart here with execution context) | |
function partial( orig_func ) { | |
var ap = Array.prototype, | |
partial_args = ap.slice.call( arguments, 1 ), | |
invoked_args; | |
function fn() { | |
var result; | |
// allow for more than just one argument to be added to invoked_args per call: | |
invoked_args = ap.concat.apply( invoked_args || partial_args.slice(), ap.slice.call( arguments) ); | |
if ( invoked_args.length < orig_func.length ) { | |
return fn; | |
} else { | |
result = orig_func.apply( this, invoked_args ); | |
invoked_args = 0; | |
return result; | |
} | |
}; | |
return fn; | |
}; | |
function a( x, y, z ) { | |
console.log( x + ' and ' + y + ' or ' + z ); | |
}; | |
a( 'x', 'y', 'z' ); // "x and y or z" | |
var b = partial( a ); | |
b('u')('v')('w'); // "u and v or w" | |
var c = partial( a, 'r' ); | |
c('s')('t'); // "r and s or t" | |
var d = partial( a, 'o', 'p' ); | |
d('q'); // "o and p or q" | |
d('n'); // "o and p or n" | |
var e = partial( a, 'w'); | |
e('t','f'); // "w and t or f" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment