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