From 96db13478359614b4881841ac359d11c49632138 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Sun, 19 May 2024 16:47:11 +0200 Subject: [PATCH] Fix .iterator logic - if there is a result iterator, use that - if there is an initial buffer, create BufferIterator and set that - otherwise set the source iterator as the result iterator --- lib/ParaSeq.rakumod | 29 +++++++++++++++-------------- t/05-head.rakutest | 4 ++-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/ParaSeq.rakumod b/lib/ParaSeq.rakumod index 3f5631f..dc72e11 100644 --- a/lib/ParaSeq.rakumod +++ b/lib/ParaSeq.rakumod @@ -133,11 +133,6 @@ class ParaSeq { $self } - # Coerce into Seq, taking into account the first buffer - method !reSeq() { - Seq.new: $!result := BufferIterator.new($!buffer, $!source); - } - # Start the async process with the first buffer and the given buffer # queuing logic method !start(&queue-buffer, int $divisibility = 1) { @@ -200,7 +195,7 @@ class ParaSeq { # Just count the number of produced values method !count() { my $iterator := self.iterator; - my int $elems = nqp::elems($!buffer); + my int $elems; nqp::until( nqp::eqaddr($iterator.pull-one, IterationEnd), ++$elems @@ -228,8 +223,13 @@ class ParaSeq { # Acts as a normal iterator, producing values from queues that are # filled asynchronously. If there is no result iterator, then the - # source iterator will be assumed - multi method iterator(ParaSeq:D:) { $!result // $!source } + # source iterator be recreated using the initial buffer if there is + # one. Otherwise + multi method iterator(ParaSeq:D:) { + $!result //= nqp::istype($!buffer,IterationBuffer) + ?? BufferIterator.new($!buffer, $!source) + !! $!source + } #- introspection --------------------------------------------------------------- @@ -465,22 +465,22 @@ class ParaSeq { proto method invert(|) {*} multi method invert(ParaSeq:D:) { - self!pass-the-chain: self!reSeq.invert.iterator + self!pass-the-chain: self.Seq.invert.iterator } proto method skip(|) {*} multi method skip(ParaSeq:D: |c) { - self!pass-the-chain: self!reSeq.skip(|c).iterator + self!pass-the-chain: self.Seq.skip(|c).iterator } proto method head(|) {*} multi method head(ParaSeq:D: |c) { - self!pass-the-chain: self!reSeq.head(|c).iterator + self!pass-the-chain: self.Seq.head(|c).iterator } proto method tail(|) {*} multi method tail(ParaSeq:D: |c) { - self!pass-the-chain: self!reSeq.tail(|c).iterator + self!pass-the-chain: self.Seq.tail(|c).iterator } proto method reverse(|) {*} @@ -504,13 +504,14 @@ class ParaSeq { !! self!count - 1 } - multi method head( ParaSeq:D:) { self!reSeq.head } - multi method tail( ParaSeq:D:) { self!reSeq.tail } + multi method head( ParaSeq:D:) { self.Seq.head } + multi method tail( ParaSeq:D:) { self.Seq.tail } multi method is-lazy(ParaSeq:D:) { $!source.is-lazy } #- coercers -------------------------------------------------------------------- + multi method IterationBuffer(ParaSeq:D:) { self.iterator.push-all(my $buffer := nqp::create(IterationBuffer)); $buffer diff --git a/t/05-head.rakutest b/t/05-head.rakutest index adb2948..2269c6c 100644 --- a/t/05-head.rakutest +++ b/t/05-head.rakutest @@ -3,8 +3,8 @@ use ParaSeq; plan 10; -my constant $elems = 20; #0000; -my constant $batch = 2; #000; +my constant $elems = 200000; +my constant $batch = 2000; my constant @list = (^$elems).List; my constant $head = @list.head;