Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^7: A short whishlist of Perl5 improvements leaping to Perl7

by eyepopslikeamosquito (Bishop)
on Nov 28, 2020 at 08:24 UTC ( #11124308=note: print w/replies, xml ) Need Help??


in reply to Re^6: A short whishlist of Perl5 improvements leaping to Perl7
in thread A short whishlist of Perl5 improvements leaping to Perl7

I'm not sure why rtoa has to be in the closure.
Just realised it doesn't thanks to the introduction of state variables in perl v5.10.
use v5.10; use strict; use warnings; use List::Util qw(reduce); sub roman_to_dec { state %rtoa = ( M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1 ); reduce { $a+$b-$a%$b*2 } map { $rtoa{$_} } split//, uc(shift); } my @testdata = ( "XLII", "LXIX", "mi" ); for my $r (@testdata) { print "$r: ", roman_to_dec($r), "\n"; }

Replies are listed 'Best First'.
Re^8: A short whishlist of Perl5 improvements leaping to Perl7
by choroba (Cardinal) on Nov 30, 2020 at 16:54 UTC
    I don't like state in named subroutines. It makes it hard to re´nitialise the variable and causes hard to debug problems. An anonymous sub is OK, because the state variable is re´nitialised every time the sub is created.

    But in this particular case, it's not highly probable you'd ever need to re´nitialise the variable. But, if you plan to convert from different digit systems, you'll quickly see why state is not the solution.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^8: A short whishlist of Perl5 improvements leaping to Perl7
by LanX (Sage) on Nov 28, 2020 at 12:12 UTC
    > > I'm not sure why rtoa has to be in the closure.

    I should have been clearer... I don't understand why you didn't go the easy way of defining my %rtoa either on file or function scope. No problem translating this to python.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      I don't understand why you didn't go the easy way of defining my %rtoa either on file or function scope.
      Though that was easier, I am pedantic and just couldn't bring myself to do it:
      1. I didn't want %rtoa at file scope because other functions in the same file have no business knowing about its existence - it is a roman_to_dec() implementation detail after all and I should be able to later change the implementation of this function with complete confidence that doing so cannot possibly break other functions in the same file ... which is why I originally created the bare block, solely to hide %rtoa from other functions in the same file.
      2. I didn't want my %rtoa at function scope because it is gratuitously inefficient to (unnecessarily) initialise %rtoa each and every time the function is called (it is a constant after all). More importantly, expressing that %rtoa is const and is used only by roman_to_dec aids code clarity (BTW, I miss C++'s const keyword when coding in Perl).
      BTW, it seems to me that state variables were added to perl expressly to solve the above two concerns.

      Update: Re the rationale for introducing state variables, found a quote from Larry himself in Re: About "state" variables in Perl 5.10:

      However, that difference is not why we introduced state variables. The big win is the psychological one of not having to look outside of the sub to find the definition of $x. (That, and we wanted it as a primitive in Perl 6 so that people could write stateful macro constructs without forcing the user to define the state variable externally; in fact, we also use it ourselves in order to implement the stateful flipflop operator without having to build the flipflop in as a primitive, as it is in Perl 5.)

        yeah, I understand your aspiration for 100% solutions.

        But it's a Rosetta Stone fire-and-forget demo script and I doubt we'd see any other function added.

        Or do you expect a need for sumerian_to_maya() soon? ;-)

        > (BTW, I miss C++'s const keyword when coding in Perl).

        why not ...

        use Readonly; Readonly my %has => (key => value, key => value, ...);

        If that's too ugly, we could try to define an attribute to do so.

        my %has :ro = (key => value, key => value, ...);

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2023-03-28 17:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (68 votes). Check out past polls.

    Notices?