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#121 · main · 16 min 23 s
0Étoiles
0Forks
40Issues
| 1 | // Parallel work: 64 actors each compute a Fibonacci number, on all |
| 2 | // threads. Spreading them over the threads is the scheduler's job. |
| 3 | |
| 4 | import std/io |
| 5 | import std/process.{type Subject, type proc} |
| 6 | |
| 7 | fn fib :: n: Int -> Int |
| 8 | { |
| 9 | if n < 2 |
| 10 | { |
| 11 | n |
| 12 | } |
| 13 | else |
| 14 | { |
| 15 | fib (n - 1) + fib (n - 2) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn start :: n: Int results: Subject<Int> -> () / <proc> |
| 20 | { |
| 21 | if n > 0 |
| 22 | { |
| 23 | let _ = process.spawn fn { process.send results (fib 32) } |
| 24 | start (n - 1) results |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | fn collect :: n: Int results: Subject<Int> acc: Int -> Int / <proc> |
| 29 | { |
| 30 | if n == 0 |
| 31 | { |
| 32 | acc |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | collect (n - 1) results (acc + process.receive_forever results) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn main :: -> () / <io, proc> |
| 41 | { |
| 42 | let results = process.subject () |
| 43 | start 64 results |
| 44 | io.println (show (collect 64 results 0)) |
| 45 | } |
| 46 | //> 139411776 |