On 11/11/05, Joe Gottman <[EMAIL PROTECTED]> wrote: > The various synopses contain many mentions of Iterators. These are used, > for instance, to implement lazy lists so the expression 1..1_000_000 does > not have to allocate a million element array. But as far as I can tell the > term is never defined anywhere in the synopses. Is Iterator a role, and if > so what methods is it required to have? Do functions like map and grep, > which in Perl5 return lists, return Iterators in Perl6? Can an Iterator be > passed to a function (like map and grep again) that requires a list as an > input? Does an array or hash have only one Iterator or can it have several > independent ones?
I'm going to speculate on a few of these items. Iterator most certainly will be a role, though that doesn't mean that internal iterators will do this role. I expect they would, though. It will probably have methods that do the following: * next_item * prev_item * nth_item * rewind * has_next_item? * has_prev_item? I expect that lists will be convertable to iterators and vice-versa. You can do this in P5 already: sub create_iterator_from_list { my @list = @_; return sub { return shift @list; }; } create_list_from_iterator { my ($iterator) = @_; my @list; while ( my $item =$iterator->() ) { push @list, $item; } return @list; } Of course, that's extremely inefficient. I expect that Perl6 will be a little smarter, though it's not clear how much smarter it can be. For one thing, Iterators will have to support two modes - one where the list is frozen (the P5 version I posted above) so that changes to list don't change the iterator and another where the iterator iterates into the list as it stands, so that changes to the list are reflected in the iterator immediately. The second mode has the possibility of being unstable, but if you choose this mode (which wouldn't be the default), then it's on your head. As for multiple iterators, that's the point of having a separate iterator object. Each iterator maintains its own state, allowing for multiple traversals across the same set. As for lazylists (1..Inf), they have to be implemented via iterators (which is why I'm almost positive the two will be interchangeable). And, if you pass (1..Inf) to map/grep/sort, it would HAVE to return an iterator back, otherwise you'll hang at that instruction. Of course, if you said "while (my $n = 1..Inf) { }", it's on your head to provide a "last" in there somewhere. Rob