// Arrays (std/array): indexing, updates that leave the original // unchanged, derived traits, and sending. The native runtime updates // unique arrays in place and copies shared ones; the program cannot tell. import std/array.{type Array} import std/io import std/list import std/process.{type Subject, type proc} @derive Eq Show Debug Hash pub type Grid { Grid(name: String, cells: Array) } pub fn main :: -> () / { basics () updates () traits () sending () } fn basics :: -> () / { let a = array.from_list [10, 20, 30] io.println ( show (array.length a) <> " " <> show (array.get a 0) <> " " <> show (array.get a 2) <> " " <> show (array.get a 3) <> " " <> show (array.get a (-1)) ) io.println ( show (array.to_list a) <> " " <> show (array.empty () == array.from_list [0]) <> " " <> show (array.length (array.empty ())) ) io.println ( show (array.slice a 1 99) <> " " <> show (array.slice a (-4) 1) <> " " <> show (array.slice a 2 1) ) io.println ( show (array.map a fn x { x / 10 }) <> " " <> show (array.fold a 0 fn acc x { acc + x }) ) let words = array.map a fn x { "w" <> show x } io.println (debug words <> " " <> show (array.fold words "" fn acc w { acc <> w })) // A big-integer index, an empty array, a counted element (inline // reads, issue #128). let huge = 4611686018427387903 * 4 let none: Array = array.empty () io.println ( show (array.get a huge) <> " " <> show (array.get none 0) <> " " <> debug (array.get words 1) ) } /// `[0, 1, ..., n - 1]`, pushed one by one (in place: `acc` is unique). fn count_up :: i: Int n: Int acc: Array -> Array { if i < n { count_up (i + 1) n (array.push acc i) } else { acc } } fn updates :: -> () / { let a = array.from_list ["a", "b", "c"] let b = array.set a 1 "B" let c = array.push a "d" io.println (show a <> " " <> show b <> " " <> show c) // Updating a unique array many times, then a shared one. let big = count_up 0 2000 (array.with_capacity 3) let shared = array.set big 0 (-1) io.println ( show (array.length big) <> " " <> show (array.get big 0) <> " " <> show (array.get shared 0) <> " " <> show (array.fold big 0 fn acc x { acc + x }) ) let nested = array.from_list [array.from_list [1], array.empty ()] let grown = array.set nested 1 (array.push (array.empty ()) 2) io.println (show nested <> " " <> show grown) } fn traits :: -> () / { let a = array.from_list [1, 2, 3] io.println ( show (a == array.from_list [1, 2, 3]) <> " " <> show (a == array.from_list [1, 2]) <> " " <> show (a < array.from_list [1, 3]) <> " " <> show (array.from_list [1, 2] < a) <> " " <> show (hash a == hash (array.push (array.from_list [1, 2]) 3)) ) let g = Grid("g", a) io.println ( show g <> " " <> debug g <> " " <> show (g == Grid("g", array.set a 0 1)) <> " " <> show (hash g == hash Grid("g", a)) ) } fn total :: inbox: Subject<(Array, Subject)> left: Int -> () / { if left > 0 { let (data, reply) = process.receive_forever inbox // The receiver's update never shows in the sender's array. let changed = array.set data 0 1000 process.send reply (array.fold changed 0 fn acc x { acc + x }) total inbox (left - 1) } else { () } } // Sent to another actor: large and kept (shared), large and not kept // (moved), small and kept (copied). fn sending :: -> () / { let inbox = process.subject () let reply = process.subject () let _ = process.spawn fn { let mine = process.subject () process.send inbox mine total mine 3 } let worker = process.receive_forever inbox let large = count_up 0 1000 (array.empty ()) process.send worker (large, reply) io.println ( "large " <> show (process.receive_forever reply) <> " " <> show (array.get large 0) ) process.send worker (count_up 0 1000 (array.empty ()), reply) io.println ("moved " <> show (process.receive_forever reply)) let small = array.from_list (list.range 1 3) process.send worker (small, reply) io.println ("small " <> show (process.receive_forever reply) <> " " <> show small) } //> 3 Some(10) Some(30) None None //> [10, 20, 30] False 0 //> [20, 30] [10] [] //> [1, 2, 3] 60 //> ["w10", "w20", "w30"] w10w20w30 //> None None Some("w20") //> [a, b, c] [a, B, c] [a, b, c, d] //> 2000 Some(0) Some(-1) 1999000 //> [[1], []] [[1], [2]] //> True False True True True //> Grid(g, [1, 2, 3]) Grid { name: "g", cells: [1, 2, 3] } True True //> large 500500 Some(0) //> moved 500500 //> small 1005 [1, 2, 3]