Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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.


Comment on Re^2: Rosetta PGA-TRAM
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://772560]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2015-07-05 08:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (61 votes), past polls