Created
February 13, 2022 16:13
-
-
Save gfldex/3875e79bc8c9225c0bed50a2cf8cc820 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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