Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make gather properly lazy, using continuations.
This eliminates the cheating implementation we've used so far.
  • Loading branch information
jnthn committed Jan 8, 2014
1 parent 33f252e commit 9333da3
Showing 1 changed file with 3 additions and 35 deletions.
38 changes: 3 additions & 35 deletions src/core/GatherIter.pm
Expand Up @@ -3,20 +3,17 @@ class GatherIter is Iterator {
has $!reified; # Parcel of this iterator's results
has $!infinite; # true if iterator is known infinite

#?if jvm
#?if !parrot
my $GATHER_PROMPT = [];
my $SENTINEL := [];
#?endif
#?if moar
my $SENTINEL := [];
#?endif
method new($block, Mu :$infinite) {
#?if parrot
my Mu $coro :=
nqp::clone(nqp::getattr(&coro, Code, '$!do'));
nqp::ifnull($coro($block), Nil);
#?endif
#?if jvm
#?if !parrot
my Mu $takings;
my Mu $state;
my sub yield() {
Expand All @@ -30,26 +27,6 @@ class GatherIter is Iterator {
$takings := $SENTINEL; yield();
};
my $coro := sub () is rw { nqp::continuationreset($GATHER_PROMPT, $state); $takings };
#?endif
#?if moar
# XXX Cheating, eager implementation for now. Limits to 1000
# takes.
my int $done = 0;
my $coro := -> {
if $done {
$SENTINEL
}
else {
my Mu $takings := nqp::list();
my int $taken = 0;
nqp::handle($block().eager(),
'TAKE', nqp::stmts(
nqp::push($takings, nqp::getpayload(nqp::exception())),
($taken = $taken + 1) <= 1000 && nqp::resume(nqp::exception())));
$done = 1;
nqp::p6parcel($takings, Mu)
}
}
#?endif
my Mu $new := nqp::create(self);
nqp::bindattr($new, GatherIter, '$!coro', $coro);
Expand Down Expand Up @@ -80,21 +57,12 @@ class GatherIter is Iterator {
#?if parrot
$end = nqp::p6bool(nqp::isnull($parcel));
#?endif
#?if jvm
$end = nqp::p6bool(nqp::eqaddr($parcel, $SENTINEL));
#?endif
#?if moar
#?if !parrot
$end = nqp::p6bool(nqp::eqaddr($parcel, $SENTINEL));
#?endif
nqp::push($rpa, $parcel) unless $end;
$count = $count - 1;
}
#?if moar
# Needed until we have proper thing-at-a-time gather/take.
if nqp::elems($rpa) {
$rpa := nqp::getattr(nqp::atpos($rpa, 0), Parcel, '$!storage');
}
#?endif
nqp::push($rpa,
nqp::p6bindattrinvres(
nqp::p6bindattrinvres(
Expand Down

0 comments on commit 9333da3

Please sign in to comment.