-
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
Allow "continue" and "break" within comprehensions, behaving as filters. #372
Comments
I suppose it's because you shouldn't be able to assign a for loop with a continue. |
I'm sure there was a very good reason why x: for i in A a() continue if c2 b() continue if c3 c() Doesn't compile to: x = (function() { _e = []; _g = A; for (_f = 0, _h = _g.length; _f < _h; _f++) { i = _g[_f]; a() if(c2) continue b() if(c3) continue _e.push((function() { return c(); })()); } return _e; })(); But I can't remember it! Can someone enlighten me? |
I don't think we really want to make continue part of a comprehension ... the three nodes: So, for your example, use a "when" clause, or just build up the results yourself:
|
I agree with weepy here. Why don't we just leave out adding any value to the comprehension if the loop hits a statement? |
It's certainly more expressive if we allow continue and break. |
Good idea. Reopening as an "enhancement"... Edit: A verbose way to get a list of even numbers, with this proposal, would be:
If we naively stop treating "break" and "continue" as pure statements, we get:
Which destroys the "continue". CoffeeScript used to have a custom strategy for converting each statement node into an expression, but using closures across the board both simplified matters and made things more reliable... So I'm not sure how feasible this ticket really is. |
I'm sorry I'm probably missing something, but I can't quite see what the inner closure provides. It seems to overcomplicate what should be a fairly simple loop. var _a, _b, _c, _d, result, x; result = _a = []; _c = list; for (_b = 0, _d = _c.length; _b < _d; _b++) { x = _c[_b]; if (x % 2 !== 0) { continue; } x; } _a; All we need is to change the last line of the loop to |
any further thoughts on this ? Do you think using a custom strategy for looping expressions is a bad idea Edit: Sorry i meant to add that I have been writing quite alot of CoffeeScript recently and personally I have found that the loops part of CS is quite complex and rather unintuitive Im sorry to say. |
I think that either We don't necessarily have to give up the loop body closures, either. Jashkenas' example above could be compiled to something like this:
|
sethaurus: I'm at a loss as to how we could start a patch to make this work... Given that:
I don't see how we can preserve the meaning of the "break", without resorting to exception-throwing tomfoolery, or multiple levels of "return" with special objects getting passed out, and checked for. It would be lovely if the semantics of JavaScript allowed you to use "break" across a function boundary, working with an external loop, and would fix the current JS nastiness with |
There must be a way get loops and comprehensions to work without closures? |
Still taking another couple looks at this, but I don't see a way to improve the current situation. sethaurus's example is great, but checking for the Closing the ticket, but will happily re-open it if someone has a complete proposal or patch. |
👍 for I was creating a comprehension in this form (oversimplified example): list = for x in [1,2,3]
if x is 1
x
else if x is 2
x I was surprised to find Coffee inserting a void(0) at the end. I would argue in this case the expected behavior would be an implicit I was able to solve it by using continue as specified in #1669: list = for x in [1,2,3]
if x is 1
x
else if x is 2
x
else continue This is counter-intuitive IMHO, but I'm sure swapping the behavior would be a breaking change for dependent code. Therefore I think the clearest path forward is |
gives
Note the final _a
The text was updated successfully, but these errors were encountered: