Created
August 15, 2016 21:00
-
-
Save ahalbert/5cd22fafc0c383c70da0fadc0a50d614 to your computer and use it in GitHub Desktop.
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 groupby(@elements is copy, $key={ $_ }) is export { | |
gather { | |
my @rest = @elements.Array; | |
while ?@rest { | |
my $head = $key(shift @rest); | |
@rest = (@rest ==> grep { $head ne $key($_)}); | |
take (@rest ==> grep { $head eq $key($_)}, $head); | |
} | |
} | |
} | |
say groupby(['a','b','a','a','a','b','b','c','a']); #((a) (b) (c)) | |
Should be ((a a a a a) (b b b) (c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment