Damian Conway writes: > > There's no second iterator. Just C<for> walking through an array. >
( questions in the form of answers :-) so : * "for" impose array context for first argument and doesnt care about "nature" of the array which it was given eventually as an argument . no multiple streams -- use parallel and friends. * parallel and friends return lazy array ( for performance considerations ???? ) _o_r_ "for" *notice* the parallel and ??? optimize it away / dont know for parallel(@a,@b) -> ($x,$y) { ... } * every object with "next" method can function as iterator ??? _o_r_ we always have to inherit from "iterator" class . what about reset/rewind ??? * $fh = open "myfile" ; for <$fh> { ... while <$fh> { ... } ... } both <$fh> *ultimately* use *the same* method call $fh.next , but second <$fh> does it explicitely , while the first -- from under the cloth of lazy array returned by $fh.each and "drived" by "for" . * what does it mean that "for walks the array" ( keeping in mind that that array may be usual or lazy and for have to not to care ) > > > > What's the difference between a lazy array and an iterator? Is there > > caching? > > Yes. A lazy array is a wrapper-plus-cache around an iterator. > $fh = open "file" ; @a := $fh ; print @a[3] # 4 calls to "$fh.next" print @a[0] # no calls to "$fh.next" is that the meaning of ...-plus-cache ???? > > > Some of questions about iterators and stuff: > > > > 1- Are iterators now considered a fundamental type? > > Probably, since they're fundamental to I/O and C<for> loops. > > so every class can define its own "next" method or inherit from Iterator to be used as an iterator _o_r_ it ( class ) *always* have to inherit from Iterator -- to be used as iterator ??? Naively , it seems that this is similar to booleans in perl -- no need to inherit from special class to behave as boolean. > > > 2a- Is there an C<Iterator.toArray()> or C<.toList()> method? > > Iterator::each. > > > > 2a1- The notion that Iterator.each() returns a lazy array seems a > > little wierd. Isn't a lazy array just an iterator? > > No. It's an array that populates itself on-demand using an iterator. > what is the difference between the arrays @a, @b , ... here ???? $a = Iterator.new( ... ) @a = $a.each ; @b := $a.each ; @c := $a ; @d is lazy = ( 1, 2, 3 ) ; @f is lazy = $a.each ; > Iterator: an object that returns successive values from some source > (such as an array, a filehandle, or a coroutine) isnt it *anything* having method "next" ??? why do I need a special type "iterator" ? thanks , arcadi .