New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move map/grep into Any class to make it accessible to Range #493
Comments
From @cspencerAfter having code such as: (1..4).map: { .say } fail because the map method wasn't accessible to the Range class, I've map/grep should work on both Lists and Ranges now. |
From @cspencerany-map-grep.patchIndex: src/builtins/any-list.pir
===================================================================
--- src/builtins/any-list.pir (revision 34129)
+++ src/builtins/any-list.pir (working copy)
@@ -24,7 +24,6 @@
'!EXPORT'('end', 'from'=>$P0)
.end
-
=item elems()
=cut
@@ -56,6 +55,38 @@
.return ($I0)
.end
+=item grep(...)
+
+=cut
+
+.sub 'grep' :method :multi(_, 'Sub')
+ .param pmc test
+ .local pmc retv
+ .local pmc iter
+ .local pmc block_res
+ .local pmc block_arg
+
+ retv = new 'List'
+ iter = self.'iterator'()
+ loop:
+ unless iter goto done
+ block_arg = shift iter
+ block_res = test(block_arg)
+
+ unless block_res goto loop
+ retv.'push'(block_arg)
+ goto loop
+
+ done:
+ .return(retv)
+.end
+
+.sub 'grep' :multi('Sub')
+ .param pmc test
+ .param pmc values :slurpy
+ .tailcall values.'grep'(test)
+.end
+
=item join
=cut
@@ -80,7 +111,60 @@
.return ($S0)
.end
+=item map()
+=cut
+
+.namespace []
+.sub 'map' :multi('Sub')
+ .param pmc expression
+ .param pmc values :slurpy
+ .tailcall values.'map'(expression)
+.end
+
+.namespace ['Any']
+.sub 'map' :method :multi(_, 'Sub')
+ .param pmc expression
+ .local pmc res, elem, block, mapres, iter, args
+ .local int i, arity
+
+ arity = expression.'arity'()
+ if arity > 0 goto body
+ arity = 1
+ body:
+ res = new 'List'
+ iter = self.'iterator'()
+ map_loop:
+ unless iter goto done
+
+ # Creates arguments for closure
+ args = new 'ResizablePMCArray'
+
+ i = 0
+ args_loop:
+ if i == arity goto invoke
+ unless iter goto elem_undef
+ elem = shift iter
+ goto push_elem
+ elem_undef:
+ elem = new 'Failure'
+ push_elem:
+ push args, elem
+ inc i
+ goto args_loop
+
+ invoke:
+ (mapres :slurpy) = expression(args :flat)
+ unless mapres goto map_loop
+ mapres.'!flatten'()
+ $I0 = elements res
+ splice res, mapres, $I0, 0
+ goto map_loop
+
+ done:
+ .return(res)
+.end
+
=item min
=cut
Index: src/classes/List.pir
===================================================================
--- src/classes/List.pir (revision 34129)
+++ src/classes/List.pir (working copy)
@@ -16,7 +16,7 @@
p6meta.'register'('ResizablePMCArray', 'parent'=>listproto, 'protoobject'=>listproto)
$P0 = get_hll_namespace ['List']
- '!EXPORT'('first,grep,keys,kv,map,pairs,reduce,values', $P0)
+ '!EXPORT'('first,keys,kv,pairs,reduce,values', $P0)
.end
=head2 Methods
@@ -375,39 +375,6 @@
.return(retv)
.end
-=item grep(...)
-
-=cut
-
-.sub 'grep' :method :multi('ResizablePMCArray', 'Sub')
- .param pmc test
- .local pmc retv
- .local pmc iter
- .local pmc block_res
- .local pmc block_arg
-
- retv = new 'List'
- iter = self.'iterator'()
- loop:
- unless iter goto done
- block_arg = shift iter
- block_res = test(block_arg)
-
- unless block_res goto loop
- retv.'push'(block_arg)
- goto loop
-
- done:
- .return(retv)
-.end
-
-.sub 'grep' :multi('Sub')
- .param pmc test
- .param pmc values :slurpy
- .tailcall values.'grep'(test)
-.end
-
-
=item iterator()
Returns an iterator for the list.
@@ -469,63 +436,6 @@
.tailcall values.'kv'()
.end
-
-=item map()
-
-Map.
-
-=cut
-
-.sub 'map' :method :multi('ResizablePMCArray', 'Sub')
- .param pmc expression
- .local pmc res, elem, block, mapres, iter, args
- .local int i, arity
-
- arity = expression.'arity'()
- if arity > 0 goto body
- arity = 1
- body:
- res = new 'List'
- iter = self.'iterator'()
- map_loop:
- unless iter goto done
-
- # Creates arguments for closure
- args = new 'ResizablePMCArray'
-
- i = 0
- args_loop:
- if i == arity goto invoke
- unless iter goto elem_undef
- elem = shift iter
- goto push_elem
- elem_undef:
- elem = new 'Failure'
- push_elem:
- push args, elem
- inc i
- goto args_loop
-
- invoke:
- (mapres :slurpy) = expression(args :flat)
- unless mapres goto map_loop
- mapres.'!flatten'()
- $I0 = elements res
- splice res, mapres, $I0, 0
- goto map_loop
-
- done:
- .return(res)
-.end
-
-
-.sub 'map' :multi('Sub')
- .param pmc expression
- .param pmc values :slurpy
- .tailcall values.'map'(expression)
-.end
-
-
=item pairs()
Return a list of Pair(index, value) elements for the invocant.
|
From @moritzOn Fri Dec 19 21:06:25 2008, cspencer wrote:
Thanks for your patch, applied as r34133. (You correctly removed map and I've also added tests to t/spec/S03-operators/range.t. This patch caused t/spec/S29-list/map_function_return_values.t to fail Cheers, |
The RT System itself - Status changed from 'new' to 'open' |
@moritz - Status changed from 'open' to 'resolved' |
From @moritzOn Sat Dec 20 02:17:09 2008, moritz wrote:
I've investigated further, and it turned out to be not the patch, but I fixed that, so no worries. Cheers, |
2 similar comments
From @moritzOn Sat Dec 20 02:17:09 2008, moritz wrote:
I've investigated further, and it turned out to be not the patch, but I fixed that, so no worries. Cheers, |
From @moritzOn Sat Dec 20 02:17:09 2008, moritz wrote:
I've investigated further, and it turned out to be not the patch, but I fixed that, so no worries. Cheers, |
Migrated from rt.perl.org#61550 (status was 'resolved')
Searchable as RT61550$
The text was updated successfully, but these errors were encountered: