Damian Conway wrote: > > The formulation of coroutines I favour doesn't work like that. > > Every time you call a suspended coroutine it resumes from immediately > after the previous C<yield> than suspended it. *And* that C<yield> > returns the new argument list with which it was resumed. > > So you can write things like: > > sub pick_no_repeats (*@from_list) { > my $seen; > while (pop @from_list) { > next when $seen; > @from_list := yield $_; > $seen |= $_; > } > } > > # and later: > > while pick_no_repeats( @values ) { > push @values, some_calc($_); > } > > Allowing the list of choices to change, but repetitions still to be avoided. >
I understand that this formulation is more powefull, but one thing I like about python's way (where a coroutine is just a funny way to generate lazy arrays) is that it lets you _use_ coroutines without even knowing what they are about. Such as when you say: for $graph.nodes { ... } ..nodes may be implemented as a coroutine, but you just don't care about it. Plus any function that previously returned an array can be reimplemented as coroutine at any time, without having to change the caller side. In other words, how do you create a lazy array of dynamically generated values in perl6? Maybe it could be something like this: $foo = &bar.instantiate(1, 2, 3); @array = $foo.as_array; -angel