Navigation Menu

Skip to content

Commit

Permalink
Implement Any.first in Perl 6
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit aae0fa1c81d0d44ce1c3a1bc6d8fd61cf983a1b9
Author: Cory Spencer <cspencer@sprocket.org>
Date:   Wed Mar 11 13:28:22 2009 -0700

    Modified fail message for first method.

commit 6ae1a7d4288510a85718cbce4ebfc4918bd5b742
Merge: f2e2a16... 9844bf0...
Author: Cory Spencer <cspencer@sprocket.org>
Date:   Wed Mar 11 13:22:24 2009 -0700

    Merge branch 'master' of git://github.com/rakudo/rakudo into any-list

commit f2e2a16a7e60b5660eba4e8c0694cc7e5397742f
Author: Cory Spencer <cspencer@sprocket.org>
Date:   Wed Mar 11 13:00:10 2009 -0700

    Changed pairs method to "is export", removed pairs sub definition.  Added a first method/sub, modified grep method to use @.list instead of $values invocant parameter.
  • Loading branch information
cspencer authored and moritz committed Mar 11, 2009
1 parent cd256dc commit 7edb29f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
39 changes: 0 additions & 39 deletions src/builtins/any-list.pir
Expand Up @@ -55,45 +55,6 @@ the size of that file down and to emphasize their generic,
.return ($I0)
.end

=item first(...)

=cut

.namespace []
.sub 'first' :multi('Sub')
.param pmc test
.param pmc values :slurpy

.tailcall values.'first'(test)
.end

.namespace ['Any']
.sub 'first' :method :multi(_, 'Sub')
.param pmc test
.local pmc retv
.local pmc iter
.local pmc block_res
.local pmc block_arg

iter = self.'iterator'()
loop:
unless iter goto nomatch
block_arg = shift iter
block_res = test(block_arg)
if block_res goto matched
goto loop

matched:
retv = block_arg
goto done

nomatch:
retv = '!FAIL'('Undefined value - first list match of no matches')

done:
.return(retv)
.end

=item join

=cut
Expand Down
20 changes: 13 additions & 7 deletions src/setting/Any-list.pm
@@ -1,7 +1,13 @@
class Any is also {
our List multi method grep($values: Code $test) {
multi method first(Code $test) {
return $_ if $test($_) for @.list;

fail('No values matched');
}

our List multi method grep(Code $test) {
gather {
take $_ if $test($_) for $values.list;
take $_ if $test($_) for @.list;
}
}

Expand Down Expand Up @@ -33,7 +39,7 @@ class Any is also {
};


multi method pairs(*@indices) {
our List multi method pairs(*@indices) is export {
gather {
if @indices {
for (@.list.keys Z @.list) -> $key, $val is rw {
Expand Down Expand Up @@ -75,6 +81,10 @@ class Any is also {
}
}

multi first(Code $test, *@values) {
@values.first($test)
}

our List multi grep(Code $test, *@values) {
@values.grep($test)
}
Expand All @@ -87,10 +97,6 @@ multi min(Code $by, *@values) {
@values.min($by);
}

our List multi pairs(@values, *@indices) {
@values.pairs(@indices)
}

multi reduce(Code $expression, *@values) {
@values.reduce($expression);
}
Expand Down

0 comments on commit 7edb29f

Please sign in to comment.