Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.