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

my @b = @{ reduce { push @$a, $b if !@$a || $b ne $a->[-1]; $a } [], @ +a };
This is really clever and should be included in List::Util: https://rt.cpan.org/Ticket/Display.html?id=150773.
Note I had to disambiguate reduce by adding a '+'.

Replies are listed 'Best First'.
Re^5: reduce like iterators
by jdporter (Paladin) on Jan 09, 2024 at 14:05 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

        The issue isn't a missing + (which doesn't help parse the code correctly); the issue is that you didn't declare reduce (which has a prototype). Add

        use List::Util qw( reduce );

        But your note still shows the former (without), not the latter (with). I guess I'm confused.