in reply to Re^5: A short whishlist of Perl5 improvements leaping to Perl7
in thread A short whishlist of Perl5 improvements leaping to Perl7
you can mimic your semantic in Python by using a generator function acting as closure, defining a nested function roman_to_dec and returning it.
I don't have Python installed and am not a Py expert, so please accept my semantically equal Perl interpolation.
(untested)
sub generator { my %rtoa = ( M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1 ); my $c_nested = sub { reduce { $a+$b-$a%$b*2 } map { $rtoa{$_} } split//, uc(sh +ift); }; return $c_nested; } *roman_to_dec = generator();
NB: This will look much easier in Python because it's automatically dereferencing.
I suppose most Pythonistas would rather prefer making rtoa a class variable and using roman_to_dec as a method. TIMTOWTDI. ;-)
Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: A short whishlist of Perl5 improvements leaping to Perl7 (Python TMTOWTDI)
by eyepopslikeamosquito (Archbishop) on Nov 26, 2020 at 06:46 UTC | |
Re^7: A short whishlist of Perl5 improvements leaping to Perl7
by eyepopslikeamosquito (Archbishop) on Nov 28, 2020 at 08:24 UTC | |
by choroba (Cardinal) on Nov 30, 2020 at 16:54 UTC | |
by LanX (Saint) on Nov 28, 2020 at 12:12 UTC | |
by eyepopslikeamosquito (Archbishop) on Nov 28, 2020 at 20:33 UTC | |
by LanX (Saint) on Nov 28, 2020 at 23:36 UTC | |
by eyepopslikeamosquito (Archbishop) on Nov 29, 2020 at 01:22 UTC |