-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Adding for/else? #3298
Comments
ref satyr/coco#75 |
I've always found Python's That being said, having an analogous syntax to Python but with a drastically different meaning doesn't sound like a good decision either. |
Better to think in coffee for this one. This would work: found = false
(do_stuff_with(x); found = true) for x in lst when cond(x)
do_other_stuff() unless found (As a mild brain teaser, does anyone think they can make this a little more tight?) Anyway, I don't think this is a feature worth adding, since there's generally a nicer and more fluent way to achieve the same thing. |
@bjmiller you could remove the first line, couldn't you? (do_stuff_with(x); found = true) for x in lst when cond(x)
do_other_stuff() unless found (Unless you reuse the |
I'm just going to put it out there; syntax for an @2rs2ts proposal seems useful, the coco issue seems 2edgy4me. |
I need for/else pretty often ( |
@Nami-Doc But you're supposed to write; It's like you don't want performant code! |
considering |
-1 Too specific of a case vs. great confusion of where the hell is the |
http://jsperf.com/empty-enumerable-loop-or-check Turns out it's inconclusive anyway. If the object is filled, the for loop is slowest, if it's not filled the for loop is fastest (I'd personally just write I also -1 as even though I run into this use case a bit, it seems unintuitive for shorthanding. |
Sorry to revive an old issue, but lots of the discussion has been around Coco's for...else semantics (which seems not especially useful), and not so much about Python's for...else semantics, which I've used a lot and find very helpful. Yes, you can use the idiom:
But the idea is to avoid having to write the idiom (2-3 lines worth), and just say
This is especially useful if there's lots of computation between the Another fun example you can do with these semantics is to
Admittedly, this is pretty unreadable... but it is convenient. :-) Perhaps another name than |
Python's for/else feature isn't good enough for us to want to add to JavaScript. For a good introduction, see: http://python-notes.curiousefficiency.org/en/latest/python_concepts/break_else.html The first section, Reasons for Confusion, is especially relevant. In any case, doing manual found = false
for item in list
if item.property is 'value'
found = item
break
if found
... Try something like: found = where list, property: 'value'
if found ... Finally, from that page:
|
Would it be possible to work in a
for/else
construct? Such as in python:Currently I'd write my coffeescript like this to do the same:
It's just a little syntactic sugar, but it'd be nice.
The text was updated successfully, but these errors were encountered: