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

roundrobin doesn't work as expected on arrays #5955

Closed
p6rt opened this issue Jan 4, 2017 · 6 comments
Closed

roundrobin doesn't work as expected on arrays #5955

p6rt opened this issue Jan 4, 2017 · 6 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Jan 4, 2017

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

Searchable as RT130498$

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

From @mscha

  my @​l = (0,1,2),(3,4,5),(6,7);
  say roundrobin(|@​l);

outputs

  (((0 1 2) (3 4 5) (6 7)))

instead of the expected

  ((0 3 6) (1 4 7) (2 5))

The following *does* work​:

  my $l = ((1,2,3),(4,5,6),(7,8));
  say roundrobin(|$l);

Note that zip (the function) has the same issue, but Z (the operator)
does not​:

  my @​l = (0,1,2),(3,4,5),(6,7);
  my $l = ((0,1,2),(3,4,5),(6,7));
  say zip(|@​l);
  say zip(|$l);
  say [Z] @​l;
  say [Z] $l;

outputs​:

  (((0 1 2) (3 4 5) (6 7)))
  ((0 3 6) (1 4 7))
  ((0 3 6) (1 4 7))
  ((0 3 6) (1 4 7))

Unfortunately, there is no operator for roundrobin (right?) that I can
use as a workaround.

Latest Rakudo Star​:

This is Rakudo version 2016.11 built on MoarVM version 2016.11
implementing Perl 6.c.

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

From @zoffixznet

On IRC I said this was a bug, because the behaviour seemed so surprising, but now that I made a fix for it (diff attached), I find breakage in t/spec/S03-metaops/zip.t and t/spec/S32-container/roundrobin.t that test for what I see is a contradictory (to this ticket) behaviour for RT#​126522.

Basically roundrobin, zip, and a few others aren't meant to treat itemized lists as lists and instead treat them as a single item. This is why the behaviour in this ticket is observed​: array's elements are itemized and so get treated differently.

So.... this is just a pitfall of sorts and isn't a bug?

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

From @zoffixznet

diff.diff
diff --git a/src/core/List.pm b/src/core/List.pm
index 4edd254..172009b 100644
--- a/src/core/List.pm
+++ b/src/core/List.pm
@@ -1593,14 +1593,14 @@ multi sub infix:<Z>(+lol) {
     return Seq.new(Rakudo::Internals.EmptyIterator) if $arity == 0;
     eager my @l = (^$arity).map: -> $i {
         my \elem = lol[$i];
-        if nqp::iscont(elem) {
-            $laze = False;
-            Rakudo::Internals.WhateverIterator((elem,))
-        }
-        else {
+        if nqp::istype(elem, Iterable) {
             $laze = False unless elem.is-lazy;
             Rakudo::Internals.WhateverIterator(elem)
         }
+        else {
+            $laze = False;
+            Rakudo::Internals.WhateverIterator((elem,))
+        }
     }
 
     gather {
@@ -1622,13 +1622,13 @@ my &zip := &infix:<Z>;
 sub roundrobin(**@lol is raw) {
     my $laze = False;
     my @iters = do for @lol -> \elem {
-        if nqp::iscont(elem) {
-            (elem,).iterator
-        }
-        else {
+        if nqp::istype(elem, Iterable) {
             $laze = True if elem.is-lazy;
             elem.iterator
         }
+        else {
+            (elem,).iterator
+        }
     }
     gather {
         while @iters {

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

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

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

From @zoffixznet

per jnthn++ https://irclog.perlgeek.de/perl6-dev/2017-01-04#i_13854591

Turns out this is not a bug, but a consequence of arrays itemizing their contents. You can still use the array, but you need to ».list on it it to de-itemize the contents; same with `zip`​:

<ZoffixW> m​: my @​l = (0,1,2),(3,4,5),(6,7); say zip @​l».list.Slip;
<camelia> rakudo-moar 40d7de​: OUTPUT«((0 3 6) (1 4 7))␤»
<ZoffixW> m​: my @​l = (0,1,2),(3,4,5),(6,7); say roundrobin @​l».list.Slip;
<camelia> rakudo-moar 40d7de​: OUTPUT«((0 3 6) (1 4 7) (2 5))␤»

@p6rt
Copy link
Author

p6rt commented Jan 4, 2017

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

@p6rt p6rt closed this as completed Jan 4, 2017
@p6rt p6rt added the Bug 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