4 ms·
Perl was my go-to language for 8 years before Ruby so I fancied a comparison. And.. Ruby is rarely as brief, but even with my Perl hat on, it seems much easier
by petercooper 12y ago
Perl was my go-to language for 8 years before Ruby so I fancied a comparison. And.. Ruby is rarely as brief, but even with my Perl hat on, it seems much easier to read, especially if you didn't write it:
Perl 6:
say " {.key}: {+.value} student{"s" if .value != 1}"
for %grade.classify(*.value.comb[0]).sort(*.key);
Ruby:
grades.group_by { |name, grade| grade[0] }
.sort
.each do |grade, names|
puts " #{grade}: #{names.length} student#{'s' if names.length != 1}"
end
Or if it had to be as 'functional' as the Perl..
puts grades.group_by { |name, grade| grade[0] }
.map { |grade, names| " #{grade}: #{names.length} student#{'s' if names.length != 1}" }
.sort.join("\n")
Both can be improved, of course! :-) I'd be keen to see a Python version actually because I know so few of its idioms but it often does well with these sorts of things as well..
- WayneS 12y agoAgreed, the new perl looks like Ruby. And I am willing to bet Ruby is more self consistent and easier to learn. (Says the perl 5 expert)
- kbenson 12y agoIn what ways do you not find Perl self consistent? There's lots or criticisms that can be leveled at Perl, but I'm not sure I follow that one.
- elektronjunge 12y agoA lot of the standard lib constructs behave completely differently in different contexts. Often subtly. For instance, @globvals = glob("*"); # returns all matches to @globvals $glob1 = glob("*"); # returns the first match and stores it in $glob1 $glob2 = glob("*.c"); # returns the second match of "*" and stores it in $glob2
- zzzcpan 12y agoConsistency is about expectations. It is possible to check if caller wants an array or a scalar and return either or a reference to either. And so nobody expects a single way of returning things across all the functions. Therefore such behavior is consistent.
- elektronjunge 12y agoHaving implicit shared state dependent on the requested type is certainly not consistent. To a reasonably experienced perl programmer one would expect the following: @globs = glob("*") # all matches $glob1 = glob("*") # the first match of "*" $glob2 = glob("*.c") # the first match of "*.c"
- zzzcpan 12y agohttp://perldoc.perl.org/perldata.html#Context http://perldoc.perl.org/perldata.html#Context
- prakashk 12y agoIt does return the values as expected. See my other reply: https://news.ycombinator.com/item?id=8730798 https://news.ycombinator.com/item?id=8730798
- kbenson 12y agoActually, it doesn't, and it's complicated. See my rather long-winded reply here: https://news.ycombinator.com/item?id=8730925 https://news.ycombinator.com/item?id=8730925
- kbenson 12y agoI agree that's the behavior you would want for that example. That is almost the behavior you get when use use File::Glob's bsd_glob function. For some reason it instead appears to return the last alphanumerically sorted match instead of the first. I've submitted a bug about that as well as the general lack of documentation on the true behavior of the CORE::glob() function, as outlined in my other comment.
- Skunkleton 12y agoI threw up a python version, and stole your group_by for the frequency portion :)