// std/table (issue #407, ADR 0073): a table any actor reads without a // message. Values are shared, never copied; a reader keeps what it read // after a replace or a remove; many readers race one writer safely. import std/io import std/list import std/process.{type Subject, type proc} import std/table.{type Table} type Note { Note(title: String, tags: List) } fn title :: found: Option -> String { match found { Some(note) -> note.title, None -> "none" } } fn reader :: pairs: Table> rounds: Int last: Int -> Bool / { if rounds == 0 { True } else { match table.get pairs "pair" { Some([n, double]) if double == 2 * n && n >= last -> reader pairs (rounds - 1) n , _ -> False } } } fn written :: pairs: Table> from: Int to: Int -> () / { if from <= to { table.put pairs "pair" [from, 2 * from] written pairs (from + 1) to } } pub fn main :: -> () / { let notes = table.new () io.println (title (table.get notes "ada")) table.put notes "ada" Note("milk", ["home"]) io.println (title (table.get notes "ada")) let kept = table.get notes "ada" table.put notes "ada" Note("tea", []) io.println "{title (table.get notes "ada")} {title kept}" table.remove notes "bob" table.remove notes "ada" io.println "{title (table.get notes "ada")} {title kept}" let other = table.new () table.put other "ada" Note("elsewhere", []) table.put notes "ada" Note("here", []) io.println "{title (table.get notes "ada")} {title (table.get other "ada")}" let pairs = table.new () table.put pairs "pair" [0, 0] let results: Subject = process.subject () list.range 1 32 |> list.each fn _ { let _ = process.spawn fn { process.send results (reader pairs 300 0) } () } written pairs 1 2000 let all_good = list.range 1 32 |> list.all fn _ { process.receive_forever results } io.println "{all_good} {table.get pairs "pair"}" table.remove pairs "pair" table.remove notes "ada" table.remove other "ada" } //> none //> milk //> tea milk //> none milk //> here elsewhere //> True Some([2000, 4000])