Skip to content

Instantly share code, notes, and snippets.

@araraloren
Last active July 7, 2017 12:57
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 araraloren/c9f53c8aecaf1272765c70b25d3c347b to your computer and use it in GitHub Desktop.
Save araraloren/c9f53c8aecaf1272765c70b25d3c347b to your computer and use it in GitHub Desktop.
A supply sample
#!/usr/bin/env perl6
sub start-server(Supply $s, &operator --> Supply) {
supply {
sub server() {
whenever $s -> $msg {
if $msg ~~ Int || (try so $msg.Int) {
emit &operator($msg.Int);
} else {
die "Need an integer!";
}
}
}
server();
}
}
sub auto-restart(Supply $s --> Supply) {
supply {
sub auto() {
whenever $s -> $msg {
say "RESULT => " ~ $msg;
QUIT {
default {
.note;
"Ready to restart server".say;
auto();
}
}
}
}
auto();
}
}
my $supplier = Supplier.new;
my $supply = $supplier.Supply;
my $server = start-server($supply, -> $v { 2 * $v.Int });
my $s = auto-restart($server);
$s.tap;
while (my $get = prompt("Input a number:")) {
$supplier.emit($get);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment