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

Suggestion: for...else (or for...empty) #75

Closed
ceymard opened this issue Jul 31, 2011 · 6 comments
Closed

Suggestion: for...else (or for...empty) #75

ceymard opened this issue Jul 31, 2011 · 6 comments
Milestone

Comments

@ceymard
Copy link

ceymard commented Jul 31, 2011

Seen in other languages,

for obj in container
    # do something
else
    # container was empty, do something else.

or

for obj of container
    # do something
empty
    # container was empty, do something else.

thoughts ?

@satyr
Copy link
Owner

satyr commented Jul 31, 2011

  • How do you compile?
  • Use cases?

@ceymard
Copy link
Author

ceymard commented Jul 31, 2011

Naive implementation :

for x of o
    do_something x
empty
    console.log "o is empty"

=>

var x, _i, _ref, _len;
for (_i = 0, _len = (_ref = o).length; _i < _len; ++_i) {
  x = _ref[_i];
  do_something(x);
}
if (_i == 0)
    console.log("o is empty");

In the case of the in iterator in its simplest form.

for x in o
    do_something x
empty
    console.log "o is empty"
var x, _seen = false;
for (x in o) {
  do_something(x);
  _seen = true;
}
if (!_seen)
   console.log("o is empty");

Use case :

for plugin of plugins
    load_plugin plugin
empty
    throw new Exception "My app is useless without plugins !"

or

for plugin_name in plugins
    load_plugin plugins[plugin_name]
empty
    throw new Exception "My app is useless without plugins !"

@ceymard
Copy link
Author

ceymard commented Jul 31, 2011

Just read that on the for...else of python. Interesting read : http://psung.blogspot.com/2007/12/for-else-in-python.html

@satyr
Copy link
Owner

satyr commented Jul 31, 2011

What should it return when else clause is executed?

r =
  for x of a
    x
  else
    y

http://psung.blogspot.com/2007/12/for-else-in-python.html

So Python's isn't like this proposal, but relative to break. Hm.

@ceymard
Copy link
Author

ceymard commented Jul 31, 2011

What should it return when else clause is executed?

y ?

if the programmer wants to keep the actual for assignation logic, he should write :

r =
   for x of a
       x
   else
       [y]

So Python's isn't like this proposal, but relative to break. Hm.

Yes, wondering too which would be more worthwhile to implement.

@satyr satyr closed this as completed in 62c59a7 Aug 2, 2011
@satyr
Copy link
Owner

satyr commented Aug 2, 2011

Done mostly as proposed; both while and for now accept else clause, run when the loop body wasn't run.

I don't think we should follow Python's, which is unintuitive and complicated to compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants