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

"do" for loop #2382

Closed
schloerke opened this issue Jun 13, 2012 · 5 comments
Closed

"do" for loop #2382

schloerke opened this issue Jun 13, 2012 · 5 comments

Comments

@schloerke
Copy link

I find myself writing the coffee-script do function inside a lot of for loops on the given item of the loop.

Current setup:

for item, itemPos in arr
  do (item, itemPos) ->
    # closure stuff

for k,v of obj
  do (k,v) ->
    # closure stuff

I propose modifying the for setup to keep the nice syntax of the for loop

dofor item, itemPos in arr
  # closure stuff

dofor k,v of obj
  # closure stuff

Both would still compile to the same functionality.

var item, itemPos, k, v, _fn, _fn1, _i, _len;

_fn = function(item, itemPos) {
  // closure stuff
};
for (itemPos = _i = 0, _len = arr.length; _i < _len; itemPos = ++_i) {
  item = arr[itemPos];
  _fn(item, itemPos);
}

_fn1 = function(k, v) {
  // closure stuff
};
for (k in obj) {
  v = obj[k];
  _fn1(k, v);
}
@paulmillr
Copy link

-1 for new syntax.

I'm using array methods for this thing which resolves problems.

[1, 2, 3, 4, 5].forEach (item, index) ->
  console.log('hello', item, index)

@connec
Copy link
Collaborator

connec commented Jun 19, 2012

Not opposed to sytax further simplifying this, but you can already put it on one line, and it makes it explicit that there's a closure there.

for item in [1, 2, 3, 4, 5] then do (item) ->
  console.log item
_ref = [1, 2, 3, 4, 5];
_fn = function(item) {
  return console.log(item);
};
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  item = _ref[_i];
  _fn(item);
}

@schloerke
Copy link
Author

@connec Now that you point this out, I was mainly looking for a single line setup that gives me closure. Thanks for the then tip! I didn't see an exact example on the intro page, but it makes perfect sense now.

Showing that it has closure with the function directly >> less syntax.

@OnesimusUnbound
Copy link

I've search for do'ed for for loop on CS site and this feature does not appear. This appears to be undocumented. Tagging #1808

@lehni
Copy link

lehni commented Aug 26, 2012

I was not aware that this was already possible:

for item in [1, 2, 3, 4, 5] then do (item) ->
  console.log item

But I would like to propose a simplification, which would make the code nicer, and might align better with the original proposal. Simply remove the need for 'then' in this case:

for item in [1, 2, 3, 4, 5] do (item) ->
  console.log item

And perhaps, this syntax would then implicitly pass on the for loop variables, e.g. this code in current notation:

for item, index in [1, 2, 3, 4, 5] then do (item, index) ->
  console.log index, item

Would become:

for item, index in [1, 2, 3, 4, 5] do ->
  console.log index, item

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

5 participants