Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement grep-index, first-index, first-rindex
Names still provisionally awaiting agreement and spec
  • Loading branch information
lizmat committed Apr 23, 2014
1 parent ad4b22b commit ed692e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
30 changes: 28 additions & 2 deletions src/core/Any.pm
Expand Up @@ -128,9 +128,23 @@ my class Any { # declared in BOOTSTRAP
method grep(Mu $test) is rw {
self.map({ $_ if $_ ~~ $test });
}
method grep-index(Mu $test) {
my $index = -1;
self.map: { $index++; $index if $_ ~~ $test };
}
method first(Mu $test) is rw {
my @results := self.grep($test);
@results ?? @results[0] !! Nil;
self.map({ return $_ if $_ ~~ $test });
Nil;
}
method first-index(Mu $test) {
my $index = -1;
self.map: { $index++; return $index if $_ ~~ $test };
Nil;
}
method first-rindex(Mu $test) {
my $index = self.elems;
self.reverse.map: { $index--; return $index if $_ ~~ $test };
Nil;
}

method join($separator = '') {
Expand Down Expand Up @@ -338,10 +352,22 @@ proto grep(|) {*}
multi grep(Mu $test, @values) { @values.grep($test) }
multi grep(Mu $test, *@values) { @values.grep($test) }

proto grep-index(|) {*}
multi grep-index(Mu $test, @values) { @values.grep-index($test) }
multi grep-index(Mu $test, *@values) { @values.grep-index($test) }

proto first(|) {*}
multi first(Mu $test, @values) { @values.first($test) }
multi first(Mu $test, *@values) { @values.first($test) }

proto first-index(|) {*}
multi first-index(Mu $test, @values) { @values.first-index($test) }
multi first-index(Mu $test, *@values) { @values.first-index($test) }

proto first-rindex(|) {*}
multi first-rindex(Mu $test, @values) { @values.first-rindex($test) }
multi first-rindex(Mu $test, *@values) { @values.first-rindex($test) }

proto join(|) { * }
multi join($sep = '', *@values) { @values.join($sep) }

Expand Down
16 changes: 0 additions & 16 deletions src/core/List.pm
Expand Up @@ -175,22 +175,6 @@ my class List does Positional { # declared in BOOTSTRAP
Any
)
}
method index(Mu $test) is rw {
my $index = -1;
self.map: {
$index++;
return $index if $_ ~~ $test;
};
Nil;
}
method rindex(Mu $test) is rw {
my $index = self.elems;
self.reverse.map: {
$index--;
return $index if $_ ~~ $test;
};
Nil;
}

method pick($n is copy = 1) {
fail "Cannot .pick from infinite list" if self.infinite; #MMD?
Expand Down

0 comments on commit ed692e3

Please sign in to comment.