
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // A partial application whose callee and arguments can be sent is a |
| 2 | // `send fn`: another actor can call it (issue #325). |
| 3 | |
| 4 | import std/io |
| 5 | import std/process.{type Subject, type proc} |
| 6 | |
| 7 | type Greeting { Greeting(prefix: String) } |
| 8 | |
| 9 | type Job { Job(f: send fn(String) -> String, reply: Subject<String>) } |
| 10 | |
| 11 | fn greet :: prefix: String suffix: String name: String -> String |
| 12 | { |
| 13 | prefix <> name <> suffix |
| 14 | } |
| 15 | |
| 16 | fn worker :: jobs: Subject<Subject<Job>> -> () / <proc> |
| 17 | { |
| 18 | let inbox = process.subject () |
| 19 | process.send jobs inbox |
| 20 | serve inbox |
| 21 | } |
| 22 | |
| 23 | fn serve :: inbox: Subject<Job> -> () / <proc> |
| 24 | { |
| 25 | let Job(f, reply) = process.receive_forever inbox |
| 26 | process.send reply (f "ada") |
| 27 | serve inbox |
| 28 | } |
| 29 | |
| 30 | fn ask :: inbox: Subject<Job> f: send fn(String) -> String -> String / <proc> |
| 31 | { |
| 32 | let reply = process.subject () |
| 33 | process.send inbox Job(f, reply) |
| 34 | process.receive_forever reply |
| 35 | } |
| 36 | |
| 37 | pub fn main :: -> () / <io, proc> |
| 38 | { |
| 39 | let jobs = process.subject () |
| 40 | let _ = process.spawn fn { worker jobs } |
| 41 | let inbox = process.receive_forever jobs |
| 42 | let greeting = Greeting("hello ") |
| 43 | io.println (ask inbox (greet greeting.prefix "!")) |
| 44 | //> hello ada! |
| 45 | let shout = greet "HEY " |
| 46 | io.println (ask inbox (shout "?")) |
| 47 | //> HEY ada? |
| 48 | } |