in reply to Re: reduce like iterators
in thread reduce like iterators

argh!!!

I missed that:

DB<3> use feature "state"; map { state $p; print $p;$p=$_} 1..3 12 DB<5> use feature "state"; for (1..3) {map { state $p; print $p;$p=$ +_} 1..3} 12312312

thanks a lot! :)

Cheers Rolf

Replies are listed 'Best First'.
Re^3: reduce like iterators
by TimToady (Parson) on Jan 03, 2011 at 21:04 UTC
    This particular problem is solved in Perl 6 by always cloning all blocks as closures the same way, so state is reset in loops as well as in map. (In fact, the for loop is defined in terms of map in Perl 6. So for loops return a list of values just like map does.) Perl 5 could probably move in the direction of fewer special-cased blocks that aren't true closures; this would have many subtle benefits. Not everything in Perl 6 can be borrowed back, but I suspect this is one of them.