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

Simpler Chaining Request #583

Closed
machineghost opened this issue May 1, 2012 · 3 comments
Closed

Simpler Chaining Request #583

machineghost opened this issue May 1, 2012 · 3 comments

Comments

@machineghost
Copy link

I realize that underscore has been around for awhile, is widely used, and as such won't be changing its basic chaining approach anytime soon. Despite that, I think that the current chaining model is needlessly verbose, which is why I think it'd be great if there was an option (either a plug-in or some sort of Underscore setting) that allowed the following alternative chain syntax:

(foo).operation1().operation2().;

In other words, this plug-in would:

  • Change the _(foo) usage of underscore to imply a .chain() call
  • When in "chained" mode, underscore would give the return value of any function an "" property, which would be set to the same value that would have been returned had the function not been in "chained" mode
    (This doesn't really have to be "
    "; it could be "$" or "v" or "val" or ... you get the idea; I just thought using underscore for this value seemed appropriate, and it gives the whole line a nice symmetry, as it starts and ends with an underscore).

This alternate syntax would take code like this:

return _(foo).chain().pluck('bars').reduce(reducer, []).compact().value();

or:

return .compact(.reduce(reducer, _.pluck(foo, 'bars')));

and replace it with:

return (foo).pluck('bars').reduce(reducer, []).compact().;

which is both clearer and simpler.

Of course, you could make the argument that if one is doing enough operations to justify chaining (3+ for me, but I'm sure others have different thresholds), the extra .chain() and .value() aren't a huge deal. And they aren't ... but just because something isn't a huge deal doesn't mean it can't be improved :-) I use underscore chaining fairly often (a testament to how useful the library is), and even a minor reduction in verbosity would have a noticeable effect on my code.

@machineghost
Copy link
Author

Bleh, GitHub ate my sample code; please imagine underscores wherever you see italics.

@machineghost
Copy link
Author

Just to clarify (now that I've read GitHub's markdown rules), my idealized line of code would be:

_(foo).operation1().operation2()._;

and my latter example would be:

return _(foo).pluck('bars').reduce(reducer, []).compact()._;

@jashkenas
Copy link
Owner

Cool idea, but we can't make Underscore's syntax backwards incompatible in this way, at this point.

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