-
-
Save pangyre/fe0688a40993a0deee9a84f99c98a04d to your computer and use it in GitHub Desktop.
Concise map-reduce in Perl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strictures; | |
use List::Util "reduce"; | |
use Path::Tiny; | |
use YAML; | |
# Given a list of filenames, return a hash of each word and the number | |
# of times it occurs. | |
my $reduced = word_count(@ARGV); | |
printf "%40s -> %d\n", $_, $reduced->{$_} | |
for sort { $reduced->{$b} <=> $reduced->{$a} } keys %{$reduced}; | |
sub word_count { | |
reduce { $a->{$b}++; $a } {}, | |
map { split /\W+/ } | |
map { lc path($_)->slurp_raw } @_; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment