-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Alternative Chaining Syntax #933
Comments
@machineghost: You can edit your own comments. Just be courteous and note at the bottom that you edited it. |
Not sure the underscore should be at the beginning - it usually means protected/private |
@michaelficarra Thanks for the tip; I've edited my post (and deleted my comment). @Nami-Doc That's a good point. I was trying to make the syntax match existing the Underscore style as much as possible, so I put the underscore at the beginning to emulate the underscore in But you are 100% correct: underscore-prefixed methods are frequently interpreted as being private (or "pseudo-private" if you want to get technical). So just for fun I tried putting the Underscore at the end:
and in fact I think it works very well! If you break it down it sort of "reads" correctly:
So personally I'd be happy with either syntax (and personally I'm going to switch to the underscore-after style myself). |
Another option might be to add a underscore property which is a clone of base underscore, only returning _() for each function.
Not very readable tho. would go the post- |
Very, very cool idea ... but I think that both the confusion with private naming conventions and the slightness of the code advantage you're going to get make it a bit of a golf-stroke-too-far for core Underscore. In terms of clarity, note that you can use
... I'd prefer the former. The absence of underscores-but-not-for-the-last-function, and the mirror of chain/value on the ends help keep things clear. Edit: Especially since this can be added non-destructively, I do think this would make for a fabulous plugin. Let me know if you end up packaging one up, and I'll spread the word. |
As suggested this mix-in is now available as "Underscore Grease": https://github.com/machineghost/Underscore-Grease. Thanks in advance for any publicity. |
Warning: This is a long ticket, but the short version of it is that:
_(someObject)._map(mapFunc).compact(); // == awesome!
Currently if one wants to combine two Underscore functions (eg. to compact the results of a map operation) one has to either use multiple `_()` calls, or else use `.chain()` and `.value()`:
Now both of those are great, but in a perfect world we'd be able to express the same idea less verbosely. After all, Underscore uses
map
, notmapABunchOfObjectsInToANewBunchOfObjects
right? But there's a reason for the current syntax: you need to specify "start chaining here" and "stop chaining here" ... or do you?What if there was a way to say "start chaining here, and stop chaining after the next method? Something like:
That's a little better, but it doesn't look so great if we have more chained functions :
But what if we could tell Underscore to start chaining as part of that first map call? In other words:
That's the right idea, but it's still doesn't look very Underscore-y to me ... what if we tweaked the syntax to look more like the original .. :
Now that, is just awesome, if I do say so myself. Just for comparison purposes:
As you can see, we get the joy of chaining without the need for special extra calls or LISP-like parenthesis usage. Oh, and did I mention that all of this can be achieved, with 100% backwards compatibility, in about 10 lines of code?
So, in summary:
P.S. Currently I don't have any unit tests or documentation written, but if this is something the Underscore team wants I'll be more than happy to provide a pull request with both (I just don't want to bother if the core concept is disliked).
P.P.S. I made a somewhat similar request in #583, but unlike that request this one is 100% backwards compatible.
The text was updated successfully, but these errors were encountered: