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

I had to disambiguate reduce by adding a '+'.

Did you?

Replies are listed 'Best First'.
Re^6: reduce like iterators
by kcott (Archbishop) on Jan 09, 2024 at 14:50 UTC
    "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.

        What you've linked as "your note" wasn't my note; it was an anonymous posting and we may never know who wrote it.

        Following your comment, I was curious. Using the verbatim code posted by AM gave "syntax error". Changing reduce to +reduce gave "syntax OK". I hope that clears up any confusion.

        I'm running v5.39.3, if that makes a difference.

        — Ken