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

make hyper operators work on nested arrays #1773

Closed
p6rt opened this issue May 21, 2010 · 8 comments
Closed

make hyper operators work on nested arrays #1773

p6rt opened this issue May 21, 2010 · 8 comments
Labels

Comments

@p6rt
Copy link

p6rt commented May 21, 2010

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

Searchable as RT75246$

@p6rt
Copy link
Author

p6rt commented May 21, 2010

From pkailasa@gmail.com

Please review the patch attached which lets hyper operators be used on
nested arrays like (1, 2, [3, 4]).

  >> perl6 -e 'my @​r = (1, 2, [3, 4]) >>+<< (4, 5, [6, 7]); say @​r.perl'
  [5, 7, [9, 11]]

  >> perl6 -e 'my @​r = (1, 2, [3, 4]); @​r>>++; say @​r.perl'
  [2, 3, [4, 5]]

More tests were added to properly compare the nested arrays (not just
comparing the stringified arrays).

Patched files​:
  src/core/metaops.pm
  t/spec/S03-metaops/hyper.t

Thanks,
Prakash Kailasa

@p6rt
Copy link
Author

p6rt commented May 21, 2010

From pkailasa@gmail.com

metaops.pm.patch
diff --git a/src/core/metaops.pm b/src/core/metaops.pm
index 7dbd1ef..dfc83c3 100644
--- a/src/core/metaops.pm
+++ b/src/core/metaops.pm
@@ -88,7 +88,11 @@ our multi sub hyper(&op, Iterable $lhs-iterable, Iterable $rhs-iterable, :$dwim-
 
     my @result;
     for @lhs Z @rhs -> $l, $r {
-        @result.push(op($l, $r));
+        if $l ~~ Iterable || $r ~~ Iterable {
+            @result.push([hyper(&op, $l.list, $r.list, :$dwim-left, :$dwim-right)]);
+        } else {
+            @result.push(op($l, $r));
+        }
     }
     @result
 }
@@ -100,7 +104,19 @@ our multi sub hyper(&op, $lhs, $rhs, :$dwim-left, :$dwim-right) {
 our multi sub hyper(&op, @arg) {
     my @result;
     for @arg {
-        @result.push(op($_));
+
+        # this should work, but isn't :(
+
+        # if $_ ~~ Iterable {
+        #     @result.push([hyper(&op, $_)]);
+        # } else {
+        #     @result.push(op($_));
+        # }
+
+        # this is terribly ugly; but works
+
+        @result.push([hyper(&op, $_)]) if $_ ~~ Iterable;
+        @result.push(op($_)) if $_ !~~ Iterable;
     }
     @result
 }

@p6rt
Copy link
Author

p6rt commented May 21, 2010

From pkailasa@gmail.com

hyper.t.patch
Index: t/spec/S03-metaops/hyper.t
===================================================================
--- t/spec/S03-metaops/hyper.t	(revision 30688)
+++ t/spec/S03-metaops/hyper.t	(working copy)
@@ -76,13 +76,13 @@
 
 { # unary postfix
         my @r = (1, 2, 3);
-        try { @r»++ };
+        @r»++;
         my @e = (2, 3, 4);
         #?pugs todo
         is(~@r, ~@e, "hyper auto increment an array");
 
         @r = (1, 2, 3);
-        try { @r>>++ };
+        @r>>++;
         @e = (2, 3, 4);
         #?pugs todo
         is(~@r, ~@e, "hyper auto increment an array ASCII notation");
@@ -223,66 +223,76 @@
 	is(~@r, ~@e, "hyper-method-call on list of user-defined objects");
 };
 
-#?rakudo skip 'unicode'
+#?rakudo todo 'unicode'
 { # distribution for unary prefix
         my @r;
         @r = -« ([1, 2], [3, [4, 5]]);
         my @e = ([-1, -2], [-3, [-4, -5]]);
         is(~@r, ~@e, "distribution for unary prefix");
+        is_deeply(@r, @e, "distribution for unary prefix, deep comparison");
 
         @r = -<< ([1, 2], [3, [4, 5]]);
         @e = ([-1, -2], [-3, [-4, -5]]);
         is(~@r, ~@e, "distribution for unary prefix, ASCII");
+        is_deeply(@r, @e, "distribution for unary prefix, ASCII, deep comparison");
 };
 
-#?rakudo skip 'unicode'
+#?rakudo todo 'unicode'
 { # distribution for unary postfix autoincrement
         my @r;
         @r = ([1, 2], [3, [4, 5]]);
-        try { @r»++ };
+        @r»++;
         my @e = ([2, 3], [4, [5, 6]]);
         #?pugs todo
         is(~@r, ~@e, "distribution for unary postfix autoincr");
+        is_deeply(@r, @e, "distribution for unary postfix autoincr, deep comparison");
 
         @r = ([1, 2], [3, [4, 5]]);
-        try { @r>>++ };
+        @r>>++;
         @e = ([2, 3], [4, [5, 6]]);
         #?pugs todo
         is(~@r, ~@e, "distribution for unary postfix autoincr, ASCII");
+        is_deeply(@r, @e, "distribution for unary postfix autoincr, ASCII, deep comparison");
 };
 
 #?DOES 3
-#?rakudo skip 'non-unicode hypers'
+#?rakudo todo 'non-unicode hypers'
 { # distribution for binary infix - ASCII
         my @r;
         @r = (1, 2, [3, 4]) >>+<< (4, 5, [6, 7]);
         my @e = (5, 7, [9, 11]);
         is(~@r, ~@e, "distribution for binary infix, same shape, ASCII");
+        is_deeply(@r, @e, "distribution for binary infix, same shape, ASCII, deep comparision");
 
         @r = (1, 2, [3, 4]) >>+>> (5, 6, 7);
         @e = (6, 8, [10, 11]);
         is(~@r, ~@e, "distribution for binary infix, dimension upgrade, ASCII");
+        is_deeply(@r, @e, "distribution for binary infix, dimension upgrade, ASCII, deep comparison");
 
         @r = ([1, 2], 3) <<+>> (4, [5, 6]);
         @e = ([5, 6], [8, 9]);
         is(~@r, ~@e, "distribution for binary infix, S03 cross-upgrade, ASCII");
+        is_deeply(@r, @e, "distribution for binary infix, S03 cross-upgrade, ASCII, deep comparison");
 };
 
 #?DOES 3
-#?rakudo skip 'unicode hypers'
+#?rakudo todo 'unicode hypers'
 { # distribution for binary infix - unicode
         my @r;
         @r = (1, 2, [3, 4]) »+« (4, 5, [6, 7]);
         my @e = (5, 7, [9, 11]);
         is(~@r, ~@e, "distribution for binary infix, same shape");
+        is_deeply(@r, @e, "distribution for binary infix, same shape, deep comparison");
 
-        @r = (1, 2, [3, 4]) »+« (5, 6, 7);
+        @r = (1, 2, [3, 4]) »+» (5, 6, 7);
         @e = (6, 8, [10, 11]);
         is(~@r, ~@e, "distribution for binary infix, dimension upgrade");
+        is_deeply(@r, @e, "distribution for binary infix, dimension upgrade, deep comparison");
 
-        @r = ([1, 2], 3) »+« (4, [5, 6]);
+        @r = ([1, 2], 3) «+» (4, [5, 6]);
         @e = ([5, 6], [8, 9]);
         is(~@r, ~@e, "distribution for binary infix, S03 cross-upgrade");
+        is_deeply(@r, @e, "distribution for binary infix, S03 cross-upgrade, deep comparison");
 };
 
 { # regression test, ensure that hyper works on arrays
@@ -338,7 +348,7 @@
     ok ?(@a »|« @b), '»|« hyperjunction evals';
     ok ?(@a >>|<< @b), '>>|<< hyperjunction evals, ASCII';
     ok ?(@a »&« @b), '»&« hyperjunction evals';
-    ok ?(@a >>&<< @b), '»&« hyperjunction evals, ASCII';
+    ok ?(@a >>&<< @b), '>>&<< hyperjunction evals, ASCII';
 }
 
 # test hypers on hashes

@p6rt
Copy link
Author

p6rt commented May 25, 2010

From pk-rakudo@kailasa.net

On Fri May 21 10​:41​:39 2010, pkailasa@​gmail.com wrote​:

Patched files​:
src/core/metaops.pm

The form '$l ~~ Iterable' in the patch now causes compilation error. It
is changed to the 'Iterable.ACCEPTS($l)' form.

Revised patch file is attached.

@p6rt
Copy link
Author

p6rt commented May 25, 2010

From pk-rakudo@kailasa.net

metaops.pm.patch
diff --git a/src/core/metaops.pm b/src/core/metaops.pm
index 7dbd1ef..b0ea763 100644
--- a/src/core/metaops.pm
+++ b/src/core/metaops.pm
@@ -88,7 +88,11 @@ our multi sub hyper(&op, Iterable $lhs-iterable, Iterable $rhs-iterable, :$dwim-
 
     my @result;
     for @lhs Z @rhs -> $l, $r {
-        @result.push(op($l, $r));
+        if Iterable.ACCEPTS($l) || Iterable.ACCEPTS($r) {
+            @result.push([ hyper(&op, $l.list, $r.list, :$dwim-left, :$dwim-right) ]);
+        } else {
+            @result.push(op($l, $r));
+        }
     }
     @result
 }
@@ -100,7 +104,19 @@ our multi sub hyper(&op, $lhs, $rhs, :$dwim-left, :$dwim-right) {
 our multi sub hyper(&op, @arg) {
     my @result;
     for @arg {
-        @result.push(op($_));
+
+        # this should work, but isn't :(
+
+        # if $_ ~~ Iterable {
+        #     @result.push([hyper(&op, $_)]);
+        # } else {
+        #     @result.push(op($_));
+        # }
+
+        # this is terribly ugly; but works
+
+        @result.push([hyper(&op, $_)]) if Iterable.ACCEPTS($_);
+        @result.push(op($_)) if Iterable.REJECTS($_);
     }
     @result
 }

@p6rt
Copy link
Author

p6rt commented May 25, 2010

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

@p6rt
Copy link
Author

p6rt commented Aug 12, 2010

From @coke

On Fri May 21 10​:41​:39 2010, pkailasa@​gmail.com wrote​:

Please review the patch attached which lets hyper operators be used on
nested arrays like (1, 2, [3, 4]).

    >> perl6 -e 'my @​r = (1, 2, [3, 4]) >>+<< (4, 5, [6, 7]); say @​r.perl'
[5, 7, [9, 11]]

>> perl6 \-e 'my @&#8203;r = \(1, 2, \[3, 4\]\); @&#8203;r>>\+\+; say @&#8203;r\.perl'
\[2, 3, \[4, 5\]\]

More tests were added to properly compare the nested arrays (not just
comparing the stringified arrays).

Patched files​:
src/core/metaops.pm
t/spec/S03-metaops/hyper.t

Thanks,
Prakash Kailasa

Prakash -

Sorry it's taken us so long to get to this patch, but it no longer applies cleanly[1]; but on the
plus side, it look like this works; closing ticket.

Thanks!

[1] Might even have been your patch that did it, and this ticket was just never closed.
--
Will "Coke" Coleda

@p6rt
Copy link
Author

p6rt commented Aug 12, 2010

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

@p6rt p6rt closed this as completed Aug 12, 2010
@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