Skip to content

Instantly share code, notes, and snippets.

@hzm-s
Created February 13, 2014 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hzm-s/8972145 to your computer and use it in GitHub Desktop.
Save hzm-s/8972145 to your computer and use it in GitHub Desktop.
PerlでRubyのEnumerable#group_by
sub group_by(&@) {
my $block = shift;
my %result;
for (@_) {
my $key = $block->($_);
$result{$key} = [] unless exists $result{$key};
push @{ $result{$key} }, $_;
}
return \%result;
}
group_by { $_ % 2 } (0..9);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment