This is one example where the chained comparison operators really pays off in making cleaner and easier to understand code. The need to declare an object just to declare a function is a dumb feature Scala carries over from Java. Using
say
the way I have used it here pales in comparison to Perl 5, but is definitely cleaner and nicer than Scala's printf. And I don't see any advantage here to having a Random class instead of a simple rand
function.Downsides to Perl 6 (other than currently lacking the CPAN glory of the Perl 5 version, so far as I know): It's definitely slower than I would like. And not only is it slower, but it is progressively slower: 16 seconds if I use (10000, 500, 1000) plays, 4 minutes and 9 seconds if I take ten times those numbers, rather than the 2 minutes 40 seconds you would expect.
Potential improvements: Maybe given when instead of those chained ifs. Wonder how that would compare performance wise?
When I initially read this I thought given and when would really make more sense anyway. I doubt it would give better performance, but it would be more perl6-y. OTOH I'd be afraid that if you were to use it the Scala guy would be all: that is objectively harder to read (sic) so worse.
ReplyDeleteI've just implemented another version for this in Perl 6 at http://daniel.ruoso.com/categoria/perl/dice-game-perl-6
ReplyDeleteDaniel's solution is much nicer than mine -- I strongly encourage anyone looking here to check it out.
ReplyDelete