Skip to content

Commit

Permalink
Make List.(uniq|squish) up to spec
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Aug 4, 2013
1 parent fe0813a commit 59e521e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/core/Any.pm
Expand Up @@ -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) }
Expand Down Expand Up @@ -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) }
Expand Down
39 changes: 34 additions & 5 deletions src/core/List.pm
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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:) {
Expand Down

0 comments on commit 59e521e

Please sign in to comment.