Skip to content
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

Closed
p6rt opened this issue Dec 20, 2008 · 8 comments
Closed

Move map/grep into Any class to make it accessible to Range #493

p6rt opened this issue Dec 20, 2008 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Dec 20, 2008

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

Searchable as RT61550$

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @cspencer

After having code such as​:

  (1..4).map​: { .say }

fail because the map method wasn't accessible to the Range class, I've
moved both map and grep into any-list.pir and put it in the Any class.

map/grep should work on both Lists and Ranges now.

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @cspencer

any-map-grep.patch
Index: 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.

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @moritz

On Fri Dec 19 21​:06​:25 2008, cspencer wrote​:

After having code such as​:

(1..4).map​: { .say }

fail because the map method wasn't accessible to the Range class, I've
moved both map and grep into any-list.pir and put it in the Any class.

map/grep should work on both Lists and Ranges now.

Thanks for your patch, applied as r34133. (You correctly removed map and
grep from the !EXPORT call in List.pir, but forgot to add them in
any-list.pir; I fixed that).

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
once for me, but when running it again I couldn't reproduce it, so I
hope it's nothing serious.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

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

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

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

@p6rt p6rt closed this as completed Dec 20, 2008
@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @moritz

On Sat Dec 20 02​:17​:09 2008, moritz wrote​:

This patch caused t/spec/S29-list/map_function_return_values.t to fail
once for me, but when running it again I couldn't reproduce it, so I
hope it's nothing serious.

I've investigated further, and it turned out to be not the patch, but
cotto's hash randomization patch to parrot that triggered a bug in the
test, where it had relied on ~%hash to return items in a particular order.

I fixed that, so no worries.

Cheers,
Moritz

2 similar comments
@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @moritz

On Sat Dec 20 02​:17​:09 2008, moritz wrote​:

This patch caused t/spec/S29-list/map_function_return_values.t to fail
once for me, but when running it again I couldn't reproduce it, so I
hope it's nothing serious.

I've investigated further, and it turned out to be not the patch, but
cotto's hash randomization patch to parrot that triggered a bug in the
test, where it had relied on ~%hash to return items in a particular order.

I fixed that, so no worries.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Dec 20, 2008

From @moritz

On Sat Dec 20 02​:17​:09 2008, moritz wrote​:

This patch caused t/spec/S29-list/map_function_return_values.t to fail
once for me, but when running it again I couldn't reproduce it, so I
hope it's nothing serious.

I've investigated further, and it turned out to be not the patch, but
cotto's hash randomization patch to parrot that triggered a bug in the
test, where it had relied on ~%hash to return items in a particular order.

I fixed that, so no worries.

Cheers,
Moritz

@p6rt p6rt added the patch label Jan 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant