Created
May 23, 2017 08:28
-
-
Save araraloren/929e60bf34989fa8a707943287a02174 to your computer and use it in GitHub Desktop.
Ping Pong in Perl6
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
#!/usr/bin/env perl6 | |
class Spawn { | |
has &.cb; | |
has @.args; | |
has $.name; | |
has $.in = Channel.new; | |
method receive() { | |
return $!in.receive; | |
} | |
multi method send(Str $name, \msg) { | |
for @*scheduler { | |
if .name eq $name { | |
.send([$!name, msg]); | |
last; | |
} | |
} | |
} | |
multi method send(\msg) { | |
$!in.send(msg); | |
} | |
method loop { | |
start { | |
&!cb(self, |@!args); | |
} | |
} | |
} | |
my @*scheduler = []; | |
sub main-loop() { | |
await Promise.allof( | |
[ @*scheduler>>.loop ] | |
); | |
} | |
sub spawn(&cb, @args, :$name) { | |
@*scheduler.push( | |
Spawn.new( | |
cb => &cb, | |
args => @args, | |
name => $name | |
) | |
); | |
} | |
&spawn(&ping, ["pong", 3], name => "ping"); | |
&spawn(&pong, [], name => "pong"); | |
&main-loop(); | |
sub ping(\s, Str $name, Int $x) { | |
for ^$x { | |
s.send($name, "pong"); | |
if s.receive -> ($ping, $msg) { | |
given $msg { | |
when /ping/ { | |
say "{$msg} received from {$ping}"; | |
} | |
} | |
} | |
} | |
s.send($name, "finish"); | |
say "ping finished!"; | |
} | |
sub pong(\s) { | |
while (True) { | |
if s.receive -> ($ping, $msg) { | |
given $msg { | |
when /finish/ { | |
say "pong finished."; | |
last; | |
} | |
when /pong/ { | |
say "{$msg} received from {$ping}"; | |
s.send($ping, "ping"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment