Re: foreach and stack, iterating from the bottom-up?



Lew <lew@xxxxxxxxxxxxx> wrote:
Andreas Leitgeb wrote:
the descendingIterator() is another argument in favour of
allowing for-loops with iterators, not just iterables.

For loops are allowed with iterators. The enhanced for loop is syntactic
sugar designed for one common use, only.

Technically 100% correct.
It's just the fact behind the "only" which I'm ranting about.

Just do:
for ( Iterator <Foo> iter = deq.descendingIterator(); iter.hasNext(); )
{ Foo foo = iter.next();

Ok, it's about equally ugly, than mine, but I admit it's more efficient.
This is the idiom, for which to avoid the syntactic sugar was added.
Even more such idioms could have easily been sugar-coated as well, but
were left out, back then in the days of 1.5, probably because those
descending iterators were only "invented" later (with 1.6) for Java.

The whole concept of the Iterable interface has one sore point, that
it cannot deal with a pluralism of ways to iterate something.
It's the same "problem" for which Comparator's exist besides
the Comparable interface, except of course, that this thread
is only about sugar coating, not about new functionality.

Extending the syntactic sugar to not be crippled by that limitation
of Iterable, would be a "good thing", imo. However, I admit that it
is *not* a necessity, as it may have looked like from my previous posting.

PS: My sample was already much smaller than the one I followed up to.

.



Relevant Pages

  • Re: Iterator / Iteratable confusion
    ... I am quite aware that multiple iterators for the same iterable (actual or ... It might be different if Python ... iterables and iterators seem distinct. ... keeping iterators a distinct subcategory of iterable. ...
    (comp.lang.python)
  • Re: itertools.izip brokeness
    ... All iterables have equal lengths ... Iterables have different lengths. ... You don't care what values occur in the other iterators ... That is a reasonable use case that is not supported by zipor izip() ...
    (comp.lang.python)
  • Re: reseting an iterator
    ... Ob is an iterator iff nexteither returns an object or raises StopIteration and continues to raise StopIteration on subsequent calls. ... the definition of iterators is given a slight complication. ... The extra burden is slight since most iterators are based on builtins or generator functions or expressions, which add the boilerplate automatically. ... In other words, a small bit of boilerplate added to iterators, mostly automatically, saves boilerplate in the use of iterators and iterables. ...
    (comp.lang.python)
  • Re: How to stop iteration with __iter__() ?
    ... StopIteration no matter how many times it's asked for the next value? ... Iterables are anything that you can iterate over. ... But not all iterables are iterators. ... execute BLOCK ...
    (comp.lang.python)
  • Re: Python 3000 idea -- + on iterables -> itertools.chain
    ... itercould be a wrapper type ... compatible" support for *iterable* algebra, ... even all iterators); the OP wanted it to work for any *iterable*. ... and that it must chain the iterables, ...
    (comp.lang.python)