Skip to content

Instantly share code, notes, and snippets.

@pangyre
Forked from pjlsergeant/gist:2556399
Last active June 16, 2019 18:51
Show Gist options
  • Save pangyre/fe0688a40993a0deee9a84f99c98a04d to your computer and use it in GitHub Desktop.
Save pangyre/fe0688a40993a0deee9a84f99c98a04d to your computer and use it in GitHub Desktop.
Concise map-reduce in Perl
#!/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