louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Row evidence across actors (ADR 0066), the `async` pattern: 4000 |
| 2 | // handlers, each in an actor of its own, run a page of 8 parts. Their |
| 3 | // `later` runs each part in a new actor, where `within` re-installs the |
| 4 | // handler of `ask` that the part, a bundled lambda, performs. The sum |
| 5 | // does not depend on the order in which actors run. |
| 6 | |
| 7 | import std/io |
| 8 | import std/process.{type Subject, type proc} |
| 9 | |
| 10 | effect ask |
| 11 | { |
| 12 | fn ask :: -> Int |
| 13 | } |
| 14 | |
| 15 | effect async<e> |
| 16 | { |
| 17 | fn later :: render: send fn() -> Int / <e> -> Int |
| 18 | } |
| 19 | |
| 20 | fn with_ask :: n: Int action: fn() -> a / <ask | e> -> a / <e> |
| 21 | { |
| 22 | handle |
| 23 | { |
| 24 | action () |
| 25 | } |
| 26 | with |
| 27 | { ask -> n } |
| 28 | } |
| 29 | |
| 30 | // Each part `action` defers runs in an actor of its own, inside `within`. |
| 31 | fn streamed :: |
| 32 | within: send fn(send fn() -> Int / <e>) -> Int |
| 33 | action: fn() -> a / <async<e>, proc | e2> |
| 34 | -> a / <proc | e2> |
| 35 | { |
| 36 | handle |
| 37 | { |
| 38 | action () |
| 39 | } |
| 40 | with |
| 41 | { later render -> |
| 42 | { |
| 43 | let results = process.subject () |
| 44 | let _ = process.spawn fn { process.send results (within render) } |
| 45 | process.receive_forever results |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // `n` parts: the `i`-th asks for the handler's number and multiplies it |
| 51 | // by `i`. |
| 52 | fn page :: n: Int -> Int / <async<<ask | r>> | r2> |
| 53 | { |
| 54 | if n == 0 |
| 55 | { |
| 56 | 0 |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | later fn { ask () * n } + page (n - 1) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Handler `k`: its parts see `k`. |
| 65 | fn handler :: k: Int parts: Int -> Int / <proc> |
| 66 | { |
| 67 | streamed (fn render { with_ask k fn { render () } }) fn { page parts } |
| 68 | } |
| 69 | |
| 70 | fn spawn_handlers :: k: Int parts: Int results: Subject<Int> -> () / <proc> |
| 71 | { |
| 72 | if k > 0 |
| 73 | { |
| 74 | let _ = process.spawn fn { process.send results (handler k parts) } |
| 75 | spawn_handlers (k - 1) parts results |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | fn sum :: results: Subject<Int> left: Int total: Int -> Int / <proc> |
| 80 | { |
| 81 | if left == 0 |
| 82 | { |
| 83 | total |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | sum results (left - 1) (total + process.receive_forever results) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | pub fn main :: -> () / <io, proc> |
| 92 | { |
| 93 | let handlers = 4000 |
| 94 | let parts = 8 |
| 95 | let results = process.subject () |
| 96 | spawn_handlers handlers parts results |
| 97 | // Handler `k` sums `k * i` for `i` in 1..8: 36 `k`. |
| 98 | io.println ("sum " <> show (sum results handlers 0)) |
| 99 | } |
| 100 | //> sum 288072000 |