Raku / old-issue-tracker

Tickets from RT
https://github.com/Raku/old-issue-tracker/issues
2 stars 1 forks source link

Add a Perl6 version of the Any first method #767

Closed p6rt closed 15 years ago

p6rt commented 15 years ago

Migrated from rt.perl.org#63794 (status was 'resolved')

Searchable as RT63794$

p6rt commented 15 years ago

From @cspencer

This patch replaces the PIR first method with a Perl 6 version.
Additionally, it adds the "is export" trait to the the pairs method
and removes the pair subroutine and modifies the grep method by
replacing $values.list with @​.list.

p6rt commented 15 years ago

From @cspencer

0001-P6-implementation-of-first.patch ```diff From 9046e937bde90576e22813d438e4fd09fbec62f0 Mon Sep 17 00:00:00 2001 From: Cory Spencer Date: Wed, 11 Mar 2009 13:31:22 -0700 Subject: [PATCH] Squashed commit of the following: commit aae0fa1c81d0d44ce1c3a1bc6d8fd61cf983a1b9 Author: Cory Spencer Date: Wed Mar 11 13:28:22 2009 -0700 Modified fail message for first method. commit 6ae1a7d4288510a85718cbce4ebfc4918bd5b742 Merge: f2e2a16... 9844bf0... Author: Cory Spencer 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 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. --- src/builtins/any-list.pir | 39 --------------------------------------- src/setting/Any-list.pm | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/builtins/any-list.pir b/src/builtins/any-list.pir index 52fd38d..279e4c6 100644 --- a/src/builtins/any-list.pir +++ b/src/builtins/any-list.pir @@ -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 diff --git a/src/setting/Any-list.pm b/src/setting/Any-list.pm index 4b9340d..1a996bf 100644 --- a/src/setting/Any-list.pm +++ b/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; } } @@ -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 { @@ -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) } @@ -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); } -- 1.6.1.3 ```
p6rt commented 15 years ago

From @moritz

Thanks for the patch, applied with slightly improved commit message as 7edb29f4145e3b8dac4240493248d86d3371baf6.

Cheers, Moritz

p6rt commented 15 years ago

The RT System itself - Status changed from 'new' to 'open'

p6rt commented 15 years ago

@moritz - Status changed from 'open' to 'resolved'