Any plan to support iterator?

19 views
Skip to first unread message

Wei Liu

unread,
Aug 1, 2012, 1:55:14 AM8/1/12
to loop...@googlegroups.com
The List comprehension is great, the expression of :

< expression > for < var > in < list > [if < filter >]

is simple and beautiful.

But I noticed that only List is valid for above expression, how about iterator?

In java world, it's very common used, e.g.

while(iterator.hasNext()){
    Object obj = iterator.next();
    ....
}

And something similar :

// JDBC
while(resultSet.next()){
  ....
}

One of the advantage of iterator is that we don't have to pre-load all items in the collection, just iterate doing things one by one, which is very helpful to reduce the memory footprint.

I know there is a workaround for this, to implement a utility method by Java to convert the iterator to a List, and then, invoke the java code in loop.

But as I mentioned above, the memory will be a potential problem.

It would be great if loop can support iterator syntax. Maybe it does, I just don't know how.

Thanks,
Leo

Dhanji R. Prasanna

unread,
Aug 1, 2012, 10:24:50 PM8/1/12
to loop...@googlegroups.com
Thanks.

Hmm that's an interesting point--how would it work though? Would it just return an iterator to the results?

It might become confusing to read if the expressions sometimes work as lists and sometimes as iterators...

Wei Liu

unread,
Aug 2, 2012, 1:42:36 AM8/2/12
to loop...@googlegroups.com
How about to introduce a new language key words, like:

doSomething(row) while resultSet.next() =>
    where
        row :  resultSet.getRow()
Reply all
Reply to author
Forward
0 new messages