tobogganSe connecter

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.

46 lignes · 798 octetsBrut · Historique
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
4import std/io
5import std/process.{type Subject, type proc}
6
7fn 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
19fn 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
28fn 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
40pub 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