Skip to content

Instantly share code, notes, and snippets.

@gfldex
Created February 13, 2022 16:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gfldex/3875e79bc8c9225c0bed50a2cf8cc820 to your computer and use it in GitHub Desktop.
use v6.d;
class Shifty does Positional does Iterable {
has Channel:D $.channel is required;
has $.open is rw = True;
method shift {
LEAVE $.open = False if self.channel.closed;
self.channel.receive;
}
method iterator {
class :: does Iterator {
has $.channel;
method pull-one {
$.channel.closed ?? IterationEnd !! $.channel.receive
}
}.new(:$.channel)
}
method Bool { $.open }
method elems { $.open ??!! 0 }
method Numeric { $.open ?? NaN !! 0 }
method is-lazy { True }
}
sub consumer(+@a) {
.say for @a;
# say @a.shift while @a
}
my Channel:D $c.=new;
start {
sleep 0.1;
$c.send(.item) for ^10;
$c.close;
}
my @shifty := Shifty.new(:channel($c));
consumer(@shifty);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment