3 ms·
Just for reference: a Perl 6 version: sub MAIN($limit = Inf) { my %bag is Bag = words.map(&lc); say "{.key}\t{.value}" for %bag.sort( {
by lizmat 8y ago
Just for reference: a Perl 6 version:
sub MAIN($limit = Inf) {
my %bag is Bag = words.map(&lc);
say "{.key}\t{.value}" for %bag.sort( {
$^b.value cmp $^a.value || $^a.key cmp $^b.key
} )[^$limit]
}
And for what it's worth, the `words` function is lazy, so it won't read all of the words into memory first.
- b2gills 8y agoI would have written the sort differently %bag.sort({ -.value, .key })