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

"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

Replies are listed 'Best First'.
Re^7: reduce like iterators
by ikegami (Patriarch) on Jan 10, 2024 at 15:45 UTC

    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 );
Re^7: reduce like iterators
by jdporter (Paladin) on Jan 09, 2024 at 16:27 UTC

    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

        wasn't my note; it was an anonymous

        omg, you are so right. I missed that. My bad. I'm so sorry. Well, I just thought it seemed weird to say "I made this change" yet show the code without the change. Not a big deal, ultimately.