Skip to content

Instantly share code, notes, and snippets.

@zoffixznet
Created November 25, 2016 13:56
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 zoffixznet/eee66dd3e56c4a509627ab80ec77f56c to your computer and use it in GitHub Desktop.
Save zoffixznet/eee66dd3e56c4a509627ab80ec77f56c to your computer and use it in GitHub Desktop.
use MONKEY;
role Iterable2 {
method flat2(Iterable:D:) {
Seq.new(class :: does Iterator {
has Iterator $!source;
has Iterator $!nested;
method new(\source) {
nqp::p6bindattrinvres(nqp::create(self),self,'$!source',source)
}
my constant NO_RESULT_YET = nqp::create(Mu);
method pull-one() is raw {
my $result := NO_RESULT_YET;
my $got;
nqp::while(
nqp::eqaddr($result, NO_RESULT_YET),
nqp::if(
$!nested,
nqp::if(
nqp::eqaddr(($got := $!nested.pull-one),IterationEnd),
($!nested := Iterator),
($result := $got)
),
nqp::if(
nqp::istype(($got := $!source.pull-one),Iterable)
&& nqp::not_i(nqp::iscont($got)),
($!nested := $got.flat.iterator),
($result := $got)
)
)
);
$result
}
method push-all($target --> IterationEnd) {
state $foo = 0;
my $got;
my $iterator := $!nested || $!source;
dd [[$iterator.pull-one], [$iterator.pull-one], [$iterator.pull-one], $iterator.pull-one];
exit;
nqp::until(
nqp::eqaddr(($got := $iterator.pull-one), IterationEnd)
&& nqp::eqaddr($iterator, $!source),
nqp::stmts(
($foo++),
nqp::if(
nqp::istype($got, IterationEnd),
nqp::stmts(($iterator := $!source)),
nqp::if(
nqp::istype($got,Iterable)
&& nqp::not_i(nqp::iscont($got)),
nqp::stmts(
(dd $foo), ($iterator := $got.flat.iterator)
),
$target.push($got),
),
)
)
);
}
}.new(self.iterator))
}
}
augment class List does Iterable2 {}
my @a := List.from-iterator: <a b c d e f g>[0..2, 3, 4].flat2.iterator;
say @a.Bool;
say @a.elems;
dd @a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment