Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: reduce like iterators

by ikegami (Patriarch)
on Jan 03, 2011 at 19:06 UTC ( [id://880238]=note: print w/replies, xml ) Need Help??


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

Oops, adjusted:

my @b = @{ reduce { push @$a, $b if !@$a || $b ne $a->[-1]; $a } [], @ +a };

If one were to make a list version of reduce, the callback would need access to three variables: The list (say $_), the state (say $a), the current value (say $b). The problem could be solved as follows:

my @b = list_reduce { push @$_, $b if !@$_ || $b ne $_->[-1]; undef } +undef, @a; my @b = list_reduce { push @$_, $b if $a || $b ne $_->[-1]; 0 } 1, @a; my @b = list_reduce { push @$_, $b if defined($b) && $b ne $a; $b } un +def, @a; my @b = list_reduce { push @$_, grep defined && $_ ne $a, $b; $b } und +ef, @a;

(@a may not start with undef for the last two to work properly.)

Update: Added everything after the first block of code.

Replies are listed 'Best First'.
Re^4: reduce like iterators
by Anonymous Monk on Jan 09, 2024 at 08:28 UTC
      I had to disambiguate reduce by adding a '+'.

      Did you?

        "Did you?"

        I did.

        Syntax error without "+":

        $ perl -MO=Deparse -e 'my @b = @{ reduce { push @$a, $b if !@$a || $b +ne $a->[-1]; $a } [], @a };' syntax error at -e line 1, near "$b if" -e had compilation errors.

        Syntax OK with "+":

        $ perl -MO=Deparse -e 'my @b = @{ +reduce { push @$a, $b if !@$a || $b + ne $a->[-1]; $a } [], @a };' my(@b) = @{do { push @$a, $b if not @$a or $b ne $a->[-1]; $a }->reduce([], @a);}; -e syntax OK

        — Ken

Re^4: reduce like iterators
by LanX (Saint) on Jan 03, 2011 at 19:13 UTC
    > > my @b = @{ reduce { push @$a, $b if @$a && $b ne $a->[-1]; $a } [], @a };

    And I said:

    > (Sure I could use $a as an array-ref for accumulation, not very elegant...)

    Cheers Rolf

      But as I've shown in the update, it's not very elegant if you don't either.

        It's a bit cleaner if ($state, @to_push) is returned.

        my @b = list_reduce { undef, !@$_ || $b ne $_->[-1] ? $b : () } undef, + @a; my @b = list_reduce { 0, !@$_ || $b ne $_->[-1] ? $b : () } 1, @a; my @b = list_reduce { $b, defined($b) && $b ne $a ? $b : () } undef, @ +a; my @b = list_reduce { $b, grep defined && $_ ne $a, $b } undef, @a;

        (@a may not start with undef for the last two to work properly.)

        This actually looks like map now (as it should).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-24 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found