Skip to content

Commit

Permalink
Add support for Supply.tail(*) and .tail(Inf)
Browse files Browse the repository at this point in the history
These appear to have been forgotten before.
  • Loading branch information
lizmat committed Jul 4, 2020
1 parent 1a91af8 commit 9ccfc90
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/core.c/Supply-coercers.pm6
Expand Up @@ -593,39 +593,34 @@
}
}
}
multi method tail(Supply:D: Int(Cool) $number) {
if $number > 1 {
my int $size = $number;
supply {
my $lastn := nqp::list;
my int $index = 0;
nqp::setelems($lastn,$number); # presize list
nqp::setelems($lastn,0);

whenever self -> \val {
nqp::bindpos($lastn,$index,val);
$index = ($index + 1) % $size;
LAST {
my int $todo = nqp::elems($lastn);
$index = 0 # start from beginning
if $todo < $size; # if not a full set
while $todo {
emit nqp::atpos($lastn,$index);
$index = ($index + 1) % $size;
$todo = $todo - 1;
}
}
}
}
}
elsif $number == 1 {
self.tail
}
else { # number <= 0, needed to keep tap open
supply {
whenever self -> \val { }
}
}
multi method tail(Supply:D: \limit) {
nqp::istype(limit,Whatever) || limit == Inf
?? self
!! limit <= 0
?? supply { whenever self -> \val { } }
!! (my int $size = limit.Int) == 1
?? self.tail
!! supply {
my $lastn := nqp::list;
my int $index = 0;
nqp::setelems($lastn,$size); # presize list
nqp::setelems($lastn,0);

whenever self -> \val {
nqp::bindpos($lastn,$index,val);
$index = ($index + 1) % $size;
LAST {
my int $todo = nqp::elems($lastn);
$index = 0 # start from beginning
if $todo < $size; # if not a full set
while $todo {
emit nqp::atpos($lastn,$index);
$index = ($index + 1) % $size;
$todo = $todo - 1;
}
}
}
}
}

method skip(Supply:D: Int(Cool) $number = 1) {
Expand Down

0 comments on commit 9ccfc90

Please sign in to comment.