Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^2: Rosetta PGA-TRAM

by John M. Dlugosz (Monsignor)
on Jun 17, 2009 at 22:33 UTC ( #772560=note: print w/replies, xml ) Need Help??

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.

Replies are listed 'Best First'.
Re^3: Rosetta PGA-TRAM
by moritz (Cardinal) on Jun 17, 2009 at 22:42 UTC
    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).

    No, the ^ twigil is only necessary in the first occurrence. That was introduced because things like this:

    my $block = { my $v = %hash{$^key}; say "The lookup {%hash{$^key}} yields $v"; };

    Would complain about the closure inside the string getting no argument, because $^key was interpreted as a formal parameter to the inner-most closure, which in this case was the one inside the string.

    Or more general, you couldn't refer to outer lexicals that happened to be self-declaring formal parameters.

    So it was decided that after $^foo occurred once, you could refer to it as $foo to, disambiguating it in inner blocks.

      I see. I must have overlooked that when I read through the diffs with the last version I studied carefully. It's so easy to overlook changed or especially removed things!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772560]
[LanX]: yeah its a religious mafia , the only two big calvinist houses are dutch and prussian. ..
[LanX]: choroba yes
[erix]: Utrecht has a beautiful old center, and less mass-tourism than Amsterdam
[erix]: next time, drop by :)
[LanX]: religion is also the reason how the house of Hanover aquire the English throne, all other candidates on succession where catholics...
[erix]: Henry VIII has a lot to answer for :P
[LanX]: erix sure next time you miss a conference nearby we drop by ...
[LanX]: did you even go to the Dutch Perl Workshop in Utrecht?
[erix]: haha ok. I'm going to Glasgow though, and make a long road trip round the isles
[erix]: [Lanx} not the last time but the one before (there have been two)

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2017-12-15 16:38 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (439 votes). Check out past polls.