| 1 |
/// Mooo's `n` times on channel `c`. |
| 2 |
def mooo(tx: Sender[String, r], n: Int32): Unit \ IO = |
| 3 |
match n { |
| 4 |
case 0 => () |
| 5 |
case x => Channel.send("Mooo!", tx); mooo(tx, x - 1) |
| 6 |
} |
| 7 |
|
| 8 |
/// Meow's `n` times on channel `c`. |
| 9 |
def meow(tx: Sender[String, r], n: Int32): Unit \ IO = |
| 10 |
match n { |
| 11 |
case 0 => () |
| 12 |
case x => Channel.send("Meow!", tx); meow(tx, x - 1) |
| 13 |
} |
| 14 |
|
| 15 |
/// Hiss'es `n` times on channel `c`. |
| 16 |
def hiss(tx: Sender[String, r], n: Int32): Unit \ IO = |
| 17 |
match n { |
| 18 |
case 0 => () |
| 19 |
case x => Channel.send("Hiss!", tx); hiss(tx, x - 1) |
| 20 |
} |
| 21 |
|
| 22 |
/// Start the animal farm... |
| 23 |
def main(): Unit \ IO = region rc { |
| 24 |
let (tx1, rx1) = Channel.buffered(rc, 10); |
| 25 |
let (tx2, rx2) = Channel.buffered(rc, 10); |
| 26 |
let (tx3, rx3) = Channel.buffered(rc, 10); |
| 27 |
spawn mooo(tx1, 0) @ rc; |
| 28 |
spawn meow(tx2, 3) @ rc; |
| 29 |
spawn hiss(tx3, 7) @ rc; |
| 30 |
select { |
| 31 |
case m <- recv(rx1) => m |> println |
| 32 |
case m <- recv(rx2) => m |> println |
| 33 |
case m <- recv(rx3) => m |> println |
| 34 |
} |
| 35 |
} |