Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created November 30, 2010 01:59
Show Gist options
  • Save cowboy/721002 to your computer and use it in GitHub Desktop.
Save cowboy/721002 to your computer and use it in GitHub Desktop.
JavaScript Curry
/*!
* JavaScript Curry - v0.1pre - 11/29/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
function curry(/* n,*/ fn /*, args...*/) {
var n,
aps = Array.prototype.slice,
orig_args = aps.call( arguments, 1 );
if ( typeof fn === 'number' ) {
n = fn;
fn = orig_args.shift();
} else {
n = fn.length;
}
return function() {
var args = orig_args.concat( aps.call( arguments ) );
return args.length < n
? curry.apply( this, [ n, fn ].concat( args ) )
: fn.apply( this, args );
};
}
/*
var i = 0;
function a( x, y, z ) {
console.log( ++i + ': ' + x + ' and ' + y + ' or ' + z );
};
a( 'x', 'y', 'z' ); // "1: x and y or z"
var b = curry( a );
b(); // nothing logged, `a` not invoked
b( 'x' ); // nothing logged, `a` not invoked
b( 'x', 'y' ); // nothing logged, `a` not invoked
b( 'x' )( 'y' ); // nothing logged, `a` not invoked
b( 'x' )( 'y' )( 'z' ); // "2: x and y or z"
b( 'x', 'y', 'z' ); // "3: x and y or z"
var c = curry( a, 'x' );
c(); // nothing logged, `a` not invoked
c( 'y' ); // nothing logged, `a` not invoked
c( 'y', 'z' ); // "4: x and y or z"
c( 'y' )( 'z' ); // "5: x and y or z"
var d = curry( c, 'y' );
d(); // nothing logged, `c` not invoked
d( 'z' ); // "6: x and y or z"
var e = curry( a, 'x', 'y' );
e(); // nothing logged, `a` not invoked
e( 'z' ); // "7: x and y or z"
var f = curry( a, 'x', 'y', 'z' );
f(); // "8: x and y or z"
var g = curry( a )( 'x' );
g(); // nothing logged, `a` not invoked
g( 'y' ); // nothing logged, `a` not invoked
g( 'y', 'z' ); // "9: x and y or z"
g( 'y' )( 'z' ); // "10: x and y or z"
var h = curry( a, 'x' )( 'y' );
h(); // nothing logged, `a` not invoked
h( 'z' ); // "11: x and y or z"
function aNoLength() {
var x = arguments[0], y = arguments[1], z = arguments[2];
console.log( ++i + ': ' + x + ' and ' + y + ' or ' + z );
};
// You must specify `n` of 3 here since aNoLength.length == 0!
var j = curry( 3, aNoLength );
j(); // nothing logged, `aNoLength` not invoked
j( 'x' ); // nothing logged, `aNoLength` not invoked
j( 'x', 'y' ); // nothing logged, `aNoLength` not invoked
j( 'x' )( 'y' ); // nothing logged, `aNoLength` not invoked
j( 'x' )( 'y' )( 'z' ); // "12: x and y or z"
j( 'x', 'y', 'z' ); // "13: x and y or z"
// And BTW, because the unsatisfied curried function continues to
// return a function, invoking that function without arguments
// keeps returning a function. So have fun!
j()()()( 'x' )()()()( 'y' )()()()( 'z' ); // "14: x and y or z"
j()()()()()( 'x', 'y' )()()()()()( 'z' ); // "15: x and y or z"
j()()()()()()()()()()()( 'x', 'y', 'z' ); // "16: x and y or z"
*/
@kflorence
Copy link

Array.prototype.slice creates shallow copies only, so be wary of working on object arguments as it will retain a pointer to the original object (as opposed to working on strings or numbers).

@cowboy
Copy link
Author

cowboy commented Dec 1, 2010

This behavior is consistent with how object arguments are passed into any JavaScript function, which is to say, by reference. Object copying in any currying or partial application context doesn't make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment