Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created July 7, 2020 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizmat/ab20653dfb09856707a45b1575768d31 to your computer and use it in GitHub Desktop.
Save lizmat/ab20653dfb09856707a45b1575768d31 to your computer and use it in GitHub Desktop.
Minimal Seq.AT-POS attempt
diff --git a/src/core.c/Seq.pm6 b/src/core.c/Seq.pm6
index 8b06b1628..f9d5f1643 100644
--- a/src/core.c/Seq.pm6
+++ b/src/core.c/Seq.pm6
@@ -5,6 +5,9 @@ my class Seq is Cool does Iterable does Sequence {
# way through. Can only be obtained once.
has Iterator $!iter;
+ # The number of values that have been produced
+ has int $!produced;
+
# The only valid way to create a Seq directly is by giving it the
# iterator it will consume and maybe memoize.
multi method new(Seq: Iterator:D $iter) {
@@ -41,6 +44,20 @@ my class Seq is Cool does Iterable does Sequence {
)
}
+ proto method AT-POS(|) {*}
+ multi method AT-POS(int $pos) is raw {
+ if $pos < $!produced {
+ die "asked $pos, which was already produced";
+ }
+ else {
+ my int $skip = $pos - $!produced;
+ $!produced = $pos;
+ $!iter.skip($skip)
+ ?? $!iter.pull-one
+ !! Nil
+ }
+ }
+
multi method Seq(Seq:D:) { self }
method Capture() {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment