Created
February 13, 2014 09:18
-
-
Save hzm-s/8972145 to your computer and use it in GitHub Desktop.
PerlでRubyのEnumerable#group_by
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
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