Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function currying #96

Closed
jeffrose opened this issue Nov 14, 2012 · 4 comments
Closed

Function currying #96

jeffrose opened this issue Nov 14, 2012 · 4 comments

Comments

@jeffrose
Copy link

Unless I overlooked it, I did not see a utility to curry functions.

var slice = Array.prototype.slice;

/*
 * Create a function by partially invoking an existing function
 * 
 * var add = function( a, b ){
 *     return a + b;
 *   },
 * 
 *   addTen = curry( add, 10 );
 * 
 * add( 1, 2 ); //--> 3
 * addTen( 1 ); //--> 11
 */

return function( fn ){
    var args = slice.call( arguments, 1 );

    return function(){
        return fn.apply( this, args.concat( slice.call( arguments ) ) );
    };
};
@millermedeiros
Copy link
Owner

function/bind can be used for currying, the only difference is that it allows setting the context (this value). addTen = bind(add, this, 10). Do you think we should add a new method?

@jeffrose
Copy link
Author

I saw bind but completely overlooked the fact it passed in arguments. That being the case, adding curry is more about convenience and syntax than adding real functionality. In fact I assume it would end up being implemented using bind.

There might be an argument that adding curry falls under the "be clear" goal since it could be argued that...

var addTen = curry( add, 10 );

... is more clear than...

var addTen = bind( add, this, 10 );

... but I don't feel that strongly about it because I think it is also reasonable to keep the API concise.

@millermedeiros
Copy link
Owner

uhm, I think we should add it. specially since with curry we would be able to change the context (this) during invocation (with bind that isn't possible).

@jeffrose
Copy link
Author

I had not thought of that. I think that's a much better argument for adding it than simply being clear.

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

No branches or pull requests

2 participants