Skip to content
Browse files

Add uniq-by, tests. Alas tests fail on infinite lazy cases in Rakudo,…

… but the code should work for finite cases.
  • Loading branch information...
1 parent 7a9c549 commit 4040177ac2ad3a2a158447b60d15ac5482b74d4c @colomon committed Aug 14, 2012
Showing with 18 additions and 0 deletions.
  1. +11 −0 lib/List/Utils.pm
  2. +7 −0 t/07-uniq-by.t
View
11 lib/List/Utils.pm
@@ -152,3 +152,14 @@ sub sorted-merge(@a, @b, &by = &infix:<cmp>) is export {
}
}
}
+
+sub uniq-by(@a, $by) is export {
+ my %seen;
+ gather for @a {
+ my $current = $by($_);
+ unless %seen{$current} {
+ take $_;
+ %seen{$current} = 1;
+ }
+ }
+}
View
7 t/07-uniq-by.t
@@ -0,0 +1,7 @@
+use Test;
+use List::Utils;
+
+is uniq-by(<A B C a b c d D e>, *.uc), <A B C d e>, "simple alphabetic test with uc";
+is uniq-by((1..* Z -2..*), *.abs)[^10], (1, -2, 3, 0, 4, 5, 6, 7, 8, 9), "test laziness with abs";
+
+done;

0 comments on commit 4040177

Please sign in to comment.
Something went wrong with that request. Please try again.