-
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
do for item in list
?
#2518
Comments
+1 a lot |
What about for num in [1, 2, 3] -> ? At least for me the symbol Edit: Not exactly a synonym. |
I would prefer to somehow allow
equivalent to
Though I think the existing
|
I assume that this would work as expected?
I agree with @epidemian that |
+1 Another variant may be for scoped i in [1,2,3]
console.log i similar to |
I am a fan of the syntax proposed by @connec. |
@jashkenas, I think that's a great idea! @lehni's and @connec's syntax That said, IMHO either syntax would be a great improvement. |
What about:
JavaScript has had iterators for a while now. |
coffeescript targets IE6. |
bump after that got asked on irc
note that this is valid |
+1 for: |
Any followups? Just found myself writing a loop like this and trying to minimize the amount of indentation thus intuited Would prefer if for tabKey, apiKey of pageApiMap then do (tabKey, apiKey) -> Could be shorthanded to for tabKey, apiKey of pageApiMap then do -> Where the |
You could almost 'merge' for (key, value) of object ->
# for key, value of object then do (key, value) ->
for (value, index) in array ->
# for value, index in array then do (value, index) ->
for (key) of object ->
# for key of object then do (key) ->
for (value) in array ->
# for value in array then do (value) ->
# This is perhaps a bit too interesting
for (value, index, a = 1) in array ->
# for value, index in array then do (value, index, a = 1) -> |
+1 for something that does this in one line. |
I ran into this today and was surprised it wasn't in the language. +1 for including in core. |
Please note that my proposal kept the sequence of the various components in line with how things work already in CS. So this:
Would become:
|
It's worth noting again that you can already do this with built-in array methods.
If you need to support IE6-8 you can apply a polyfill as described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill |
It's not just about the presence of I'd definitely rather see |
+1 for this in core. I'm continually surprised that I have overwritten earlier variables by (poorly) choosing the same name for a loop variable somewhere later. I also run into this doing object deconstruction assignment, i.e.
Now
|
|
It would be equivalent to: foo = for item in items then do (item) -> proc item
# or
foo = ((do (item) -> proc item) for item in items) |
A pull request for this would be welcome. |
I think for CS2 the right solution to the actual problem here would be a for let i in [1..5]
setTimeout -> console.log i
for let key, value of object
... Another option would be to take the proposal here and combine it with #4514 to produce the same output. |
Just gonna drop a link to #959 (comment) - the original issue adding |
I was going to propose Is this issue closed or patches welcome? |
Let's leave this closed — it's been long enough that we don't need to be adding a new iteration construct at this point... |
An interesting proposal raised by @lehni, here:
#2382 (comment)
... is to somehow combine the
do
andfor
keywords to give you a quick and easy way to write a loop that closes over its variables ... without hiding the fact that you're working with an implied inner function, and all of the scope changes that involves. I think it might work out nicely.... becomes the equivalent of:
Any ideas?
The text was updated successfully, but these errors were encountered: