Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Make List.(uniq|squish) up to spec

  • Loading branch information...
commit 59e521ecb2b5f799ba33cd9429b13528c5d98b08 1 parent fe0813a
@lizmat lizmat authored
Showing with 36 additions and 7 deletions.
  1. +2 −2 src/core/Any.pm
  2. +34 −5 src/core/List.pm
View
4 src/core/Any.pm
@@ -35,7 +35,7 @@ my class Any {
# derived from .list
method elems() { self.list.elems }
method end() { self.list.end }
- method uniq() { self.list.uniq }
+ method uniq(|c) { self.list.uniq(|c) }
method squish(|c) { self.list.squish(|c) }
method pick($n = 1) { self.list.pick($n) }
method roll($n = 1) { self.list.roll($n) }
@@ -832,7 +832,7 @@ multi categorize( $test, *@items ) { {}.categorize-list( $test, @items ) }
#}
proto uniq(|) { * }
-multi uniq(*@values) { @values.uniq }
+multi uniq(*@values, |c) { @values.uniq(|c) }
proto squish(|) { * }
multi squish(*@values, |c) { @values.squish(|c) }
View
39 src/core/List.pm
@@ -375,9 +375,8 @@ my class List does Positional { # declared in BOOTSTRAP
$tpos >= +$tseq;
}
- # This needs a way of taking a user-defined comparison
- # specifier, but AFAIK nothing has been spec'd yet.
- method uniq() {
+ proto method uniq(|) {*}
+ multi method uniq() {
my $seen := nqp::hash();
my str $which;
map {
@@ -391,11 +390,27 @@ my class List does Positional { # declared in BOOTSTRAP
}
}, @.list;
}
+ multi method uniq( :&as! ) {
+ my $seen := nqp::hash();
+ my str $which;
+ map {
+ $which = &as($_);
+ if nqp::existskey($seen, $which) {
+ Nil;
+ }
+ else {
+ nqp::bindkey($seen, $which, 1);
+ $_;
+ }
+ }, @.list;
+ }
+
my @secret;
- method squish(:&with = &[===]) {
+ proto method squish(|) {*}
+ multi method squish() {
my $last = @secret;
map {
- if with($_,$last) {
+ if $_ === $last {
Nil;
}
else {
@@ -404,6 +419,20 @@ my class List does Positional { # declared in BOOTSTRAP
}
}, @.list;
}
+ multi method squish( :&as! ) {
+ my $last = @secret;
+ my str $which;
+ map {
+ $which = &as($_);
+ if $which === $last {
+ Nil;
+ }
+ else {
+ $last = $which;
+ $_;
+ }
+ }, @.list;
+ }
multi method gist(List:D:) { join ' ', map { $_.gist }, @(self) }
multi method perl(List:D \SELF:) {
Please sign in to comment.
Something went wrong with that request. Please try again.