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

Added Curry and Partial Methods #474

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,12 @@ $(document).ready(function() {
equal(testAfter(0, 0), 1, "after(0) should fire immediately");
});

asyncTest("functions: partial", 2, function() {
var delayed = false;
var partialTimeout = _.partial(setTimeout, undefined, 100);
partialTimeout(function(){ delayed = true });
setTimeout(function(){ ok(!delayed, "didn't delay the function quite yet"); }, 50);
setTimeout(function(){ ok(delayed, 'delayed the function'); start(); }, 150);
});

});
15 changes: 14 additions & 1 deletion underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@
// optionally). Binding with arguments is also known as `curry`.
// Delegates to **ECMAScript 5**'s native `Function.bind` if available.
// We check for `func.bind` first, to fail fast when `func` is undefined.
_.bind = function bind(func, context) {
_.bind = _.curry = function bind(func, context) {
var bound, args;
if (func.bind === nativeBind && nativeBind) return nativeBind.apply(func, slice.call(arguments, 1));
if (!_.isFunction(func)) throw new TypeError;
Expand Down Expand Up @@ -606,6 +606,19 @@
};
};

// Transforms a function that takes multiple arguments in such a wat it can
// be called with initial set of arguments, setting "undefined" for arguments
// we don't need now, and he called with the rest of arguments.
_.partial = function(func){
var args = slice.call(arguments, 1);
return function(){
for (var i = 0, arg = 0; i < args.length && arg < arguments.length; i++)
if (args[i] === void 0) args[i] = arguments[arg++]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer if(args[i] !== _) .... That way _ is used as a skipped argument so that someValue, where it just happens to be undefined, is allowed as an argument.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would lead to some confusion cause "_" is underscore object itself and it will be passed as a parameter several times

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why it's perfect. fn(_, _, 3)(1, 2).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I came to this issue tracker specifically to request a partial function; way to read my mind! :)

return func.apply(this, args);
};
};


// Object Functions
// ----------------

Expand Down