On Sat, 18 Sep 2004, Luke Palmer wrote:
Example above becomes:
sub MediansBy5 ([EMAIL PROTECTED]) { while @list.length >= 5 { emit (sort @list.splice(0,5))[2]; }}
That's actually a very good idea. That's why Perl 6 has it :-)
sub MediansBy5 ([EMAIL PROTECTED]) { gather { while @list >= 5 { # there's no .length; it's .elems take (sort @list.splice(0,5))[2]; } }
C<gather> returns a list of everything that was C<take>n inside of it. It does this by building a coroutine out of its argument, so it works lazily.
I suppose that this construct may be used in a wider range of circumstance and will allow some other extra features too (C<untake>?), OTOH the simplicity ogf C<emit> was indeed striking. The cmt about its short length in comparison with C<return>'s one is worth taking into account though...
Michele -- l'Italia e' una penisola bagnata da tre mari e prosciugata da tremonti - "chronos12" su it.hobby.umorismo