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

push ( @soln, $val ) if ( $soln[-1] ne $val );

The brackets around the arguments to push are unnecessary, even in the full-blown if-block you used. With postfix-if you can lose the other brackets too:

push @soln, $val if $soln[-1] ne $val;

I know some folks like the extra brackets but I find removing them aids clarity.


🦛

Replies are listed 'Best First'.
Re^3: reduce like iterators
by talexb (Chancellor) on Jan 11, 2024 at 23:28 UTC

    Sure, but then you're left with

      push @soln, $val if ( $soln[-1] ne $val );
    and with the comma suggesting do this thing, then do that other thing, it could read like this:
      push @soln # and then .. $val if ( $soln[-1] ne $val );
    Huh. That can't be right.

    You could also use indentation to make it clearer ..

      push @soln, $val if ( $soln[-1] ne $val );

    Anyway, it's a matter of taste, and my preferences comes from writing C in the 80's.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      That's why I prefer

      $soln[-1] ne $val and push @soln, $val;