Skip to content

Instantly share code, notes, and snippets.

@lizmat
Created May 21, 2019 12:54
Show Gist options
  • Save lizmat/69faa8cb0f2d4972f17561b21bace743 to your computer and use it in GitHub Desktop.
Save lizmat/69faa8cb0f2d4972f17561b21bace743 to your computer and use it in GitHub Desktop.
Implementation of feed logic
use nqp;
sub PROCESS-FEED($source, @stages --> Seq:D) {
# process a stage with given code for given iterator
my class Process does Iterator {
has &.code;
has $.iterator;
has $.is-lazy;
method pull-one() {
nqp::if(
nqp::eqaddr((my \pulled := $!iterator.pull-one),IterationEnd),
IterationEnd,
&!code(pulled)
)
}
method skip-one() {
nqp::not_i(nqp::eqaddr($!iterator.pull-one,IterationEnd))
}
}
# set up the stages
my $iterator = $source.iterator;
my $is-lazy = $iterator.is-lazy;
for @stages -> &code {
$iterator = Process.new(:&code, :$iterator, :$is-lazy);
}
# return the result
Seq.new($iterator)
}
for PROCESS-FEED( ^10, ( { say "foo: $_"; 2 * $_ }, { say "bar: $_"; 3 * $_ })) {
say "baz: $_"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment