Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: reduce like iterators

by Anonymous Monk
on Jan 12, 2011 at 15:52 UTC ( [id://881924]=note: print w/replies, xml ) Need Help??


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

Any reason not to just do:
my $p; my @new = grep { $p = $_ or 1 if $p ne $_ } @orig;
?

Replies are listed 'Best First'.
Re^3: reduce like iterators
by furry_marmot (Pilgrim) on Jan 15, 2011 at 23:48 UTC
    Ummm....because I didn't think of that? :-) Thanks! You just expanded my understanding of what you can do with grep.

    --marmot

Re^3: reduce like iterators
by LanX (Saint) on Jan 16, 2011 at 11:22 UTC
    Try @orig starting with undef.

    Also see the OP for the "semi-predicate problem" discussion.

    If this seems too theoretical for you, consider the practical task to do of a run-length encoding of sparsely set arrays. (undef is a real value)

    Cheers Rolf

      Good point about the initial value -- I forgot about that.

      It can be specially handled decently, though, something like:

      my @new = my $p = shift @orig; push @new, grep { $p = $_ or 1 if $p ne $_ } @orig;

      Shoot! I totally get the semi-predicate problem, and you stated the problem clearly in the OP, but I thought I had a brainwave and rewrote my UPDATED code, which had actually accounted for the problem.

      So anyway, I re-read the entire thread and I sort of get why you'd want, say, a $^PRE special variable. But it is so trivial, I don't see why you'd bother. You are simply dealing with a list in pairwise fashion, using a previous value to evaluate a current value. Eliminating adjacent dupes is trivial...

      @new = map { $p ne $_ ? $p = $_ : () } @orig;
      except for an initial undefined value, but only because $p is initially undefined. So you define it and it works. And it's easy to remember.
      $p = 'supercalifragilistic'; @new = map { $p ne $_ ? $p = $_ : () } @orig;
      You could define a uniq_adj function...
      use strict; sub uniq_adj { my $p = 'supercalifragilistic'; return map { $p ne $_ ? $p = $_ : () } @_; } my @orig = (undef, undef, qw(a a b b c 0 c d d u u 0 0 "0" "0" '0' + '0')); my @new = uniq_adj @orig; print "'", join ("' '", @new), "'\n"; __END__ Prints --> '' 'a' 'b' 'c' '0' 'c' 'd' 'u' '0' '"0"' ''0''

      I printed with single quotes to show where undef's are being printed. Note that 0, "0", and '0' are preserved. It's very generalizable. I predefined $p, but isn't that less work than having Perl do it through a built-in? I read through the entire thread again and it seems to fit the bill. Is this kind of what you're looking for?

      Regardless, cheers!

      --marmot
        Your surely aware that replacing undef with a "magic" string is not a solution of the semi-predicate problem, because the magic string can also be an element of the processed list.

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-19 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found