| @@ -0,0 +1,99 @@ |
| 1 | +// Arrays (std/array): indexing, updates that leave the original |
| 2 | +// unchanged, derived traits, and sending. The native runtime updates |
| 3 | +// unique arrays in place and copies shared ones; the program cannot tell. |
| 4 | +import std/io |
| 5 | +import std/list |
| 6 | +import std/array.{type Array} |
| 7 | +import std/process.{type proc, type Subject} |
| 8 | + |
| 9 | +@derive(Eq, Show, Debug, Hash) |
| 10 | +pub type Grid { Grid(name: String, cells: Array<Int>) } |
| 11 | + |
| 12 | +pub fn main() -> () / <io, proc> { |
| 13 | + basics() |
| 14 | + updates() |
| 15 | + traits() |
| 16 | + sending() |
| 17 | +} |
| 18 | + |
| 19 | +fn basics() -> () / <io> { |
| 20 | + let a = array.from_list([10, 20, 30]) |
| 21 | + 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))) |
| 22 | + io.println(show(array.to_list(a)) <> " " <> show(array.empty() == array.from_list([0])) <> " " <> show(array.length(array.empty()))) |
| 23 | + io.println(show(array.slice(a, 1, 99)) <> " " <> show(array.slice(a, -4, 1)) <> " " <> show(array.slice(a, 2, 1))) |
| 24 | + io.println(show(array.map(a, fn(x) { x / 10 })) <> " " <> show(array.fold(a, 0, fn(acc, x) { acc + x }))) |
| 25 | + let words = array.map(a, fn(x) { "w" <> show(x) }) |
| 26 | + io.println(debug(words) <> " " <> show(array.fold(words, "", fn(acc, w) { acc <> w }))) |
| 27 | +} |
| 28 | + |
| 29 | +/// `[0, 1, ..., n - 1]`, pushed one by one (in place: `acc` is unique). |
| 30 | +fn count_up(i: Int, n: Int, acc: Array<Int>) -> Array<Int> { |
| 31 | + if i < n { count_up(i + 1, n, array.push(acc, i)) } else { acc } |
| 32 | +} |
| 33 | + |
| 34 | +fn updates() -> () / <io> { |
| 35 | + let a = array.from_list(["a", "b", "c"]) |
| 36 | + let b = array.set(a, 1, "B") |
| 37 | + let c = array.push(a, "d") |
| 38 | + io.println(show(a) <> " " <> show(b) <> " " <> show(c)) |
| 39 | + // Updating a unique array many times, then a shared one. |
| 40 | + let big = count_up(0, 2000, array.with_capacity(3)) |
| 41 | + let shared = array.set(big, 0, -1) |
| 42 | + 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 }))) |
| 43 | + let nested = array.from_list([array.from_list([1]), array.empty()]) |
| 44 | + let grown = array.set(nested, 1, array.push(array.empty(), 2)) |
| 45 | + io.println(show(nested) <> " " <> show(grown)) |
| 46 | +} |
| 47 | + |
| 48 | +fn traits() -> () / <io> { |
| 49 | + let a = array.from_list([1, 2, 3]) |
| 50 | + 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)))) |
| 51 | + let g = Grid("g", a) |
| 52 | + io.println(show(g) <> " " <> debug(g) <> " " <> show(g == Grid("g", array.set(a, 0, 1))) <> " " <> show(hash(g) == hash(Grid("g", a)))) |
| 53 | +} |
| 54 | + |
| 55 | +fn total(inbox: Subject<(Array<Int>, Subject<Int>)>, left: Int) -> () / <proc> { |
| 56 | + if left > 0 { |
| 57 | + let (data, reply) = process.receive_forever(inbox) |
| 58 | + // The receiver's update never shows in the sender's array. |
| 59 | + let changed = array.set(data, 0, 1000) |
| 60 | + process.send(reply, array.fold(changed, 0, fn(acc, x) { acc + x })) |
| 61 | + total(inbox, left - 1) |
| 62 | + } else { |
| 63 | + () |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// Sent to another actor: large and kept (shared), large and not kept |
| 68 | +// (moved), small and kept (copied). |
| 69 | +fn sending() -> () / <io, proc> { |
| 70 | + let inbox = process.subject() |
| 71 | + let reply = process.subject() |
| 72 | + let _ = process.spawn(fn() { |
| 73 | + let mine = process.subject() |
| 74 | + process.send(inbox, mine) |
| 75 | + total(mine, 3) |
| 76 | + }) |
| 77 | + let worker = process.receive_forever(inbox) |
| 78 | + let large = count_up(0, 1000, array.empty()) |
| 79 | + process.send(worker, (large, reply)) |
| 80 | + io.println("large " <> show(process.receive_forever(reply)) <> " " <> show(array.get(large, 0))) |
| 81 | + process.send(worker, (count_up(0, 1000, array.empty()), reply)) |
| 82 | + io.println("moved " <> show(process.receive_forever(reply))) |
| 83 | + let small = array.from_list(list.range(1, 3)) |
| 84 | + process.send(worker, (small, reply)) |
| 85 | + io.println("small " <> show(process.receive_forever(reply)) <> " " <> show(small)) |
| 86 | +} |
| 87 | +//> 3 Some(10) Some(30) None None |
| 88 | +//> [10, 20, 30] False 0 |
| 89 | +//> [20, 30] [10] [] |
| 90 | +//> [1, 2, 3] 60 |
| 91 | +//> ["w10", "w20", "w30"] w10w20w30 |
| 92 | +//> [a, b, c] [a, B, c] [a, b, c, d] |
| 93 | +//> 2000 Some(0) Some(-1) 1999000 |
| 94 | +//> [[1], []] [[1], [2]] |
| 95 | +//> True False True True True |
| 96 | +//> Grid(g, [1, 2, 3]) Grid { name: "g", cells: [1, 2, 3] } True True |
| 97 | +//> large 500500 Some(0) |
| 98 | +//> moved 500500 |
| 99 | +//> small 1005 [1, 2, 3] |