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.

81 lignes · 2181 octetsBrut · Historique
1// std/table (issue #407, ADR 0073): a table any actor reads without a
2// message. Values are shared, never copied; a reader keeps what it read
3// after a replace or a remove; many readers race one writer safely.
4
5import std/io
6import std/list
7import std/process.{type Subject, type proc}
8import std/table.{type Table}
9
10type Note { Note(title: String, tags: List<String>) }
11
12fn title :: found: Option<Note> -> String
13{
14 match found { Some(note) -> note.title, None -> "none" }
15}
16
17fn reader :: pairs: Table<List<Int>> rounds: Int last: Int -> Bool / <proc>
18{
19 if rounds == 0
20 {
21 True
22 }
23 else
24 {
25 match table.get pairs "pair"
26 { Some([n, double]) if double == 2 * n && n >= last ->
27 reader pairs (rounds - 1) n
28 , _ -> False
29 }
30 }
31}
32
33fn written :: pairs: Table<List<Int>> from: Int to: Int -> () / <proc>
34{
35 if from <= to
36 {
37 table.put pairs "pair" [from, 2 * from]
38 written pairs (from + 1) to
39 }
40}
41
42pub fn main :: -> () / <io, proc>
43{
44 let notes = table.new ()
45 io.println (title (table.get notes "ada"))
46 table.put notes "ada" Note("milk", ["home"])
47 io.println (title (table.get notes "ada"))
48 let kept = table.get notes "ada"
49 table.put notes "ada" Note("tea", [])
50 io.println "{title (table.get notes "ada")} {title kept}"
51 table.remove notes "bob"
52 table.remove notes "ada"
53 io.println "{title (table.get notes "ada")} {title kept}"
54 let other = table.new ()
55 table.put other "ada" Note("elsewhere", [])
56 table.put notes "ada" Note("here", [])
57 io.println "{title (table.get notes "ada")} {title (table.get other "ada")}"
58 let pairs = table.new ()
59 table.put pairs "pair" [0, 0]
60 let results: Subject<Bool> = process.subject ()
61 list.range 1 32
62 |> list.each
63 fn _
64 {
65 let _ = process.spawn fn { process.send results (reader pairs 300 0) }
66 ()
67 }
68 written pairs 1 2000
69 let all_good =
70 list.range 1 32 |> list.all fn _ { process.receive_forever results }
71 io.println "{all_good} {table.get pairs "pair"}"
72 table.remove pairs "pair"
73 table.remove notes "ada"
74 table.remove other "ada"
75}
76//> none
77//> milk
78//> tea milk
79//> none milk
80//> here elsewhere
81//> True Some([2000, 4000])