Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: reduce like iterators

by JavaFan (Canon)
on Jan 03, 2011 at 19:05 UTC ( [id://880237]=note: print w/replies, xml ) Need Help??


in reply to reduce like iterators

You have to be careful with any state solution. They'll work fine until you go through the same code path again (due to a loop, the sub it's in called again, etc). Being a state variable, it'll retain whatever was in it last. So you have to be careful you aren't comparing the last element of the first list with the first element of the second list.
@cleaned = @uncleaned[0, grep {@uncleaned[$i] ne @uncleaned[$i-1]} 1.. +$#uncleaded];
It needs some additional hackery to deal with undefs correctly.

Replies are listed 'Best First'.
Re^2: reduce like iterators
by LanX (Saint) on Jan 03, 2011 at 19:11 UTC
    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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://880237]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-04-23 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found