http://www.perlmonks.org?node_id=772560


in reply to Re: Rosetta PGA-TRAM
in thread Rosetta PGA-TRAM

Why does the block use $^a in one place (the automatic parameter) and $a in another place? Shouldn't that be a different (undeclared) variable? (Likewise for b).

If you move the uc you can avoid copying the whole string. Not much savings for a small string I suppose, but you break the clean pipeline nature by copying the input first, rather than doing nothing more than passing each character through the pipeline.

$x.split('').map({ %rtoa{$_.uc} }).reduce: { $^a+$^b-$a%$b*2 }
Here's a shaken-up version, starting with noticing how the list return of a for loop is similar to the map function:
   sub infix:<ↀ> ($a,$b) { $a+$b-$a%$b*2 }
   [ↀ] do for $x.split('') { %rtoa{$_.uc} }
and an excuse to show off the cool form of the reduction metaoperator.

P.S.: used pre instead of code because Perl Monks still doesn't like Unicode. code escapes out the entities, and the form isn't submitted in UTF-8 so various things are automatically encoded. Nasty.