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

Sequence operator supports limit function with arity > 1 #2373

Closed
p6rt opened this issue Feb 17, 2011 · 9 comments
Closed

Sequence operator supports limit function with arity > 1 #2373

p6rt opened this issue Feb 17, 2011 · 9 comments
Labels

Comments

@p6rt
Copy link

p6rt commented Feb 17, 2011

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

Searchable as RT84362$

@p6rt
Copy link
Author

p6rt commented Feb 17, 2011

From dwhipp@google.com

Attached patches add tests and implementation for sequence operator with
limit-function arity > 1 (the function is assumed to be false until we have
enough values to call it). The tests show how this can be used to terminate
a sequence when it converges, or when a specific curvature is seen​:

  1, */2 ... abs(*-*) < 0.01; ## convergence​: 8 values
  0,1,2,1,0,1,2 ...^ { $^a-$^b > $^b-$^c }; ## curvature​: 5 values
  1,2 ... { @​_ == 4 }; ## length​: 4 values

@p6rt
Copy link
Author

p6rt commented Feb 17, 2011

From dwhipp@google.com

0001-tests-of-sequence-operator-with-limit-function-havin.patch
From 608d6c1dc96b81e0537d670d9949451c227f5708 Mon Sep 17 00:00:00 2001
From: Dave Whipp <dwhipp@google.com>
Date: Thu, 17 Feb 2011 12:08:45 -0800
Subject: [PATCH] tests of sequence operator with limit function having arity > 1

---
 S03-sequence/limit-arity-2-or-more.t |   37 ++++++++++++++++++++++++++++++++++
 1 files changed, 37 insertions(+), 0 deletions(-)
 create mode 100644 S03-sequence/limit-arity-2-or-more.t

diff --git a/S03-sequence/limit-arity-2-or-more.t b/S03-sequence/limit-arity-2-or-more.t
new file mode 100644
index 0000000..5524ea8
--- /dev/null
+++ b/S03-sequence/limit-arity-2-or-more.t
@@ -0,0 +1,37 @@
+use v6;
+use Test;
+
+# L<S03/List infix precedence/"the sequence operator">
+
+plan 8;
+
+# sequence with a limit function of arity 2
+
+is (8,*/2 ... abs(*-*) < 2).join(', '), '8, 4, 2, 1', 'arity-2 convergence limit';
+is (8,*/2 ...^ abs(*-*) < 2).join(', '), '8, 4, 2', 'arity-2 excluded convergence limit';
+
+# sequence with a limit function of arity 3
+
+{
+  my $i = -5;
+  my @seq = { ++$i; $i**3-9*$i } ... { ($^a-$^b) > ($^b-$^c) };
+  is @seq.join(', '), '-28, 0, 10, 8, 0, -8, -10', 'arity-3 curvature limit';
+}
+
+{
+  my $i = -5;
+  my @seq = { ++$i; $i**3-9*$i } ...^ { ($^a-$^b) > ($^b-$^c) };
+  is @seq.join(', '), '-28, 0, 10, 8, 0, -8', 'arity-3 excluded curvature limit';
+}
+
+# limit functions that limit sequence exactly at arity limit
+
+is (2, 1, 0.5 ... abs(*-*) < 2).join(', '), '2, 1', 'ASAP arity-2 convergence limit';
+is (2, 1, 0.5 ...^ abs(*-*) < 2).join(', '), '2', 'ASAP arity-2 excluded convergence limit';
+
+# limit function that accepts any number of args
+
+is (1 ... { @_ eq "1 2 3" }).join(', '), '1, 2, 3', 'arity-Inf limit';
+is (1 ...^ { @_ eq "1 2 3" }).join(', '), '1, 2', 'arity-Inf excluded limit';
+
+done;
-- 
1.7.3.1

@p6rt
Copy link
Author

p6rt commented Feb 17, 2011

From dwhipp@google.com

0001-Sequence-operator-supports-limit-functions-with-arit.patch
From de547c7c44810e1cd8d03c023cc380498bff6301 Mon Sep 17 00:00:00 2001
From: Dave Whipp <dwhipp@google.com>
Date: Thu, 17 Feb 2011 12:07:27 -0800
Subject: [PATCH] Sequence operator supports limit functions with arity > 1

---
 src/core/operators.pm |   30 ++++++++++++++++++++++--------
 1 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/src/core/operators.pm b/src/core/operators.pm
index c92e9f2..074db9f 100644
--- a/src/core/operators.pm
+++ b/src/core/operators.pm
@@ -401,16 +401,30 @@ our sub _HELPER_generate-series(@lhs, $rhs , :$exclude-limit) {
     my $limit = ($rhs ~~ Whatever ?? Any !! $rhs);
     return infinite-series(@lhs , $limit) if $rhs ~~ Whatever; #shortcut infinite series so we avoid the comparisions
 
-    fail ('Limit arity cannot be larger than 1') if 	$limit ~~ Code && $limit.count > 1;
+    #fail ('Limit arity cannot be larger than 1') if   $limit ~~ Code && $limit.count > 1;
     my $series = infinite-series(@lhs , $limit);
+
     gather {
-        while $series {
-            my $val = $series.shift();
-            if $val ~~  $limit {
-                take $val unless $exclude-limit ;
-                last ;
-            };
-            take $val;
+        if $limit ~~ Code && $limit.count > 1 {
+            my @limit-args;
+            while $series {
+                @limit-args.shift if @limit-args == $limit.count;
+                my $val = $series.shift;
+                @limit-args.push($val);
+                my $done = @limit-args >= $limit.arity && $limit(|@limit-args);
+                take $val unless $done && $exclude-limit;
+                last if $done;
+            }
+        }
+        else {
+            while $series {
+                my $val = $series.shift();
+                if $val ~~ $limit {
+                    take $val unless $exclude-limit ;
+                    last ;
+                };
+                take $val;
+            }
         }
     }
 }
-- 
1.7.3.1

@p6rt
Copy link
Author

p6rt commented Feb 25, 2011

From dwhipp@google.com

One extra patch for this change​: an error if the limit-function is a multi
(not sure if you'll want to apply this as-is​: I couldn't figure out why
"fail" didn't result in an error. so used "die", instead).

On Thu, Feb 17, 2011 at 2​:35 PM, perl6 via RT
<perl6-bugs-followup@​perl.org>wrote​:

Greetings,

This message has been automatically generated in response to the
creation of a trouble ticket regarding​:
"[PATCH] Sequence operator supports limit function with arity > 1",
a summary of which appears below.

There is no need to reply to this message right now. Your ticket has been
assigned an ID of [perl #​84362].

Please include the string​:

    \[perl #&#8203;84362\]

in the subject line of all future correspondence about this issue. To do
so,
you may reply to this message.

                   Thank you,
                   perl6\-bugs\-followup@&#8203;perl\.org

-------------------------------------------------------------------------
Attached patches add tests and implementation for sequence operator with
limit-function arity > 1 (the function is assumed to be false until we have
enough values to call it). The tests show how this can be used to terminate
a sequence when it converges, or when a specific curvature is seen​:

1, */2 ... abs(*-*) < 0.01; ## convergence​: 8 values
0,1,2,1,0,1,2 ...^ { $^a-$^b > $^b-$^c }; ## curvature​: 5 values
1,2 ... { @​_ == 4 }; ## length​: 4 values

@p6rt
Copy link
Author

p6rt commented Feb 25, 2011

From dwhipp@google.com

0001-add-error-for-use-of-Multi-as-sequence-limit.patch
From 2312784b7a147bf2613b3e95f5ae37ed8dbbccaa Mon Sep 17 00:00:00 2001
From: Dave Whipp <dwhipp@google.com>
Date: Fri, 25 Feb 2011 09:27:03 -0800
Subject: [PATCH] add error for use of Multi as sequence limit

---
 src/core/operators.pm |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/core/operators.pm b/src/core/operators.pm
index 074db9f..980a86e 100644
--- a/src/core/operators.pm
+++ b/src/core/operators.pm
@@ -401,7 +401,7 @@ our sub _HELPER_generate-series(@lhs, $rhs , :$exclude-limit) {
     my $limit = ($rhs ~~ Whatever ?? Any !! $rhs);
     return infinite-series(@lhs , $limit) if $rhs ~~ Whatever; #shortcut infinite series so we avoid the comparisions
 
-    #fail ('Limit arity cannot be larger than 1') if   $limit ~~ Code && $limit.count > 1;
+    die 'Sequence limit cannot be a multi-sub or multi-method' if $limit ~~ Multi;
     my $series = infinite-series(@lhs , $limit);
 
     gather {
-- 
1.7.3.1

@p6rt
Copy link
Author

p6rt commented Feb 25, 2011

From @moritz

On Thu Feb 17 14​:35​:14 2011, dwhipp@​google.com wrote​:

Attached patches add tests and implementation for sequence operator with
limit-function arity > 1 (the function is assumed to be false until we
have
enough values to call it).

Thank you for your patches, I've applied all three.

If you tell me your github ID I can also give you commit access to roast
(the spectest repo) and other repos under the 'perl6' organization.

Cheers,
Moritz

@p6rt
Copy link
Author

p6rt commented Feb 25, 2011

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

@p6rt
Copy link
Author

p6rt commented Feb 25, 2011

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

@p6rt p6rt closed this as completed Feb 25, 2011
@p6rt
Copy link
Author

p6rt commented Feb 27, 2011

From dwhipp@google.com

Thank you.

My github ID is "dwhipp".

Dave.

On Fri, Feb 25, 2011 at 11​:56 AM, Moritz Lenz via RT <
perl6-bugs-followup@​perl.org> wrote​:

On Thu Feb 17 14​:35​:14 2011, dwhipp@​google.com wrote​:

Attached patches add tests and implementation for sequence operator with
limit-function arity > 1 (the function is assumed to be false until we
have
enough values to call it).

Thank you for your patches, I've applied all three.

If you tell me your github ID I can also give you commit access to roast
(the spectest repo) and other repos under the 'perl6' organization.

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