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.
Dernier runRéussi#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 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 | |
| 5 | import std/array.{type Array} |
| 6 | import std/io |
| 7 | import std/list |
| 8 | import std/process.{type Subject, type proc} |
| 9 | |
| 10 | @derive Eq Show Debug Hash |
| 11 | pub type Grid { Grid(name: String, cells: Array<Int>) } |
| 12 | |
| 13 | pub fn main :: -> () / <io, proc> |
| 14 | { |
| 15 | basics () |
| 16 | updates () |
| 17 | traits () |
| 18 | sending () |
| 19 | } |
| 20 | |
| 21 | fn basics :: -> () / <io> |
| 22 | { |
| 23 | let a = array.from_list [10, 20, 30] |
| 24 | io.println |
| 25 | ( show (array.length a) <> " " <> show (array.get a 0) <> " " |
| 26 | <> show (array.get a 2) |
| 27 | <> " " |
| 28 | <> show (array.get a 3) |
| 29 | <> " " |
| 30 | <> show (array.get a (-1)) |
| 31 | ) |
| 32 | io.println |
| 33 | ( show (array.to_list a) <> " " |
| 34 | <> show (array.empty () == array.from_list [0]) |
| 35 | <> " " |
| 36 | <> show (array.length (array.empty ())) |
| 37 | ) |
| 38 | io.println |
| 39 | ( show (array.slice a 1 99) <> " " <> show (array.slice a (-4) 1) <> " " |
| 40 | <> show (array.slice a 2 1) |
| 41 | ) |
| 42 | io.println |
| 43 | ( show (array.map a fn x { x / 10 }) <> " " |
| 44 | <> show (array.fold a 0 fn acc x { acc + x }) |
| 45 | ) |
| 46 | let words = array.map a fn x { "w" <> show x } |
| 47 | io.println |
| 48 | (debug words <> " " <> show (array.fold words "" fn acc w { acc <> w })) |
| 49 | // A big-integer index, an empty array, a counted element (inline |
| 50 | // reads, issue #128). |
| 51 | let huge = 4611686018427387903 * 4 |
| 52 | let none: Array<Int> = array.empty () |
| 53 | io.println |
| 54 | ( show (array.get a huge) <> " " <> show (array.get none 0) <> " " |
| 55 | <> debug (array.get words 1) |
| 56 | ) |
| 57 | } |
| 58 | |
| 59 | /// `[0, 1, ..., n - 1]`, pushed one by one (in place: `acc` is unique). |
| 60 | fn count_up :: i: Int n: Int acc: Array<Int> -> Array<Int> |
| 61 | { |
| 62 | if i < n |
| 63 | { |
| 64 | count_up (i + 1) n (array.push acc i) |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | acc |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | fn updates :: -> () / <io> |
| 73 | { |
| 74 | let a = array.from_list ["a", "b", "c"] |
| 75 | let b = array.set a 1 "B" |
| 76 | let c = array.push a "d" |
| 77 | io.println (show a <> " " <> show b <> " " <> show c) |
| 78 | // Updating a unique array many times, then a shared one. |
| 79 | let big = count_up 0 2000 (array.with_capacity 3) |
| 80 | let shared = array.set big 0 (-1) |
| 81 | io.println |
| 82 | ( show (array.length big) <> " " <> show (array.get big 0) <> " " |
| 83 | <> show (array.get shared 0) |
| 84 | <> " " |
| 85 | <> show (array.fold big 0 fn acc x { acc + x }) |
| 86 | ) |
| 87 | let nested = array.from_list [array.from_list [1], array.empty ()] |
| 88 | let grown = array.set nested 1 (array.push (array.empty ()) 2) |
| 89 | io.println (show nested <> " " <> show grown) |
| 90 | } |
| 91 | |
| 92 | fn traits :: -> () / <io> |
| 93 | { |
| 94 | let a = array.from_list [1, 2, 3] |
| 95 | io.println |
| 96 | ( show (a == array.from_list [1, 2, 3]) <> " " |
| 97 | <> show (a == array.from_list [1, 2]) |
| 98 | <> " " |
| 99 | <> show (a < array.from_list [1, 3]) |
| 100 | <> " " |
| 101 | <> show (array.from_list [1, 2] < a) |
| 102 | <> " " |
| 103 | <> show (hash a == hash (array.push (array.from_list [1, 2]) 3)) |
| 104 | ) |
| 105 | let g = Grid("g", a) |
| 106 | io.println |
| 107 | ( show g <> " " <> debug g <> " " <> show (g == Grid("g", array.set a 0 1)) |
| 108 | <> " " |
| 109 | <> show (hash g == hash Grid("g", a)) |
| 110 | ) |
| 111 | } |
| 112 | |
| 113 | fn total :: inbox: Subject<(Array<Int>, Subject<Int>)> left: Int -> () / <proc> |
| 114 | { |
| 115 | if left > 0 |
| 116 | { |
| 117 | let (data, reply) = process.receive_forever inbox |
| 118 | // The receiver's update never shows in the sender's array. |
| 119 | let changed = array.set data 0 1000 |
| 120 | process.send reply (array.fold changed 0 fn acc x { acc + x }) |
| 121 | total inbox (left - 1) |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | () |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Sent to another actor: large and kept (shared), large and not kept |
| 130 | // (moved), small and kept (copied). |
| 131 | fn sending :: -> () / <io, proc> |
| 132 | { |
| 133 | let inbox = process.subject () |
| 134 | let reply = process.subject () |
| 135 | let _ = |
| 136 | process.spawn |
| 137 | fn |
| 138 | { |
| 139 | let mine = process.subject () |
| 140 | process.send inbox mine |
| 141 | total mine 3 |
| 142 | } |
| 143 | let worker = process.receive_forever inbox |
| 144 | let large = count_up 0 1000 (array.empty ()) |
| 145 | process.send worker (large, reply) |
| 146 | io.println |
| 147 | ( "large " <> show (process.receive_forever reply) <> " " |
| 148 | <> show (array.get large 0) |
| 149 | ) |
| 150 | process.send worker (count_up 0 1000 (array.empty ()), reply) |
| 151 | io.println ("moved " <> show (process.receive_forever reply)) |
| 152 | let small = array.from_list (list.range 1 3) |
| 153 | process.send worker (small, reply) |
| 154 | io.println |
| 155 | ("small " <> show (process.receive_forever reply) <> " " <> show small) |
| 156 | } |
| 157 | //> 3 Some(10) Some(30) None None |
| 158 | //> [10, 20, 30] False 0 |
| 159 | //> [20, 30] [10] [] |
| 160 | //> [1, 2, 3] 60 |
| 161 | //> ["w10", "w20", "w30"] w10w20w30 |
| 162 | //> None None Some("w20") |
| 163 | //> [a, b, c] [a, B, c] [a, b, c, d] |
| 164 | //> 2000 Some(0) Some(-1) 1999000 |
| 165 | //> [[1], []] [[1], [2]] |
| 166 | //> True False True True True |
| 167 | //> Grid(g, [1, 2, 3]) Grid { name: "g", cells: [1, 2, 3] } True True |
| 168 | //> large 500500 Some(0) |
| 169 | //> moved 500500 |
| 170 | //> small 1005 [1, 2, 3] |