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 | // std/array's sort and sort_by (issue #380): edge cases, stability, the |
| 2 | // patterns the algorithm detects (sorted, reversed, organ-pipe, low |
| 3 | // cardinality), a custom and an effectful comparator, and a shared |
| 4 | // array left alone (the sort works on a copy, as `set` would). |
| 5 | // |
| 6 | // The pattern cases print a position-weighted checksum of the sorted |
| 7 | // output, which pins the exact sequence (stability included) without |
| 8 | // depending on how std/list's sort is implemented. |
| 9 | |
| 10 | import std/array |
| 11 | import std/io |
| 12 | import std/list |
| 13 | import std/prng.{type Prng} |
| 14 | import std/u64 |
| 15 | |
| 16 | fn randoms :: g: Prng left: Int acc: List<Int> -> List<Int> |
| 17 | { |
| 18 | if left == 0 |
| 19 | { |
| 20 | acc |
| 21 | } |
| 22 | else |
| 23 | { |
| 24 | let (x, g) = prng.next g |
| 25 | randoms |
| 26 | g |
| 27 | (left - 1) |
| 28 | [u64.to_int (u64.and x (u64.from_int 2147483647)), ..acc] |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /// The same values in `0..k`. |
| 33 | fn lows :: g: Prng left: Int k: Int acc: List<Int> -> List<Int> |
| 34 | { |
| 35 | if left == 0 |
| 36 | { |
| 37 | acc |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | let (x, g) = prng.next g |
| 42 | lows g (left - 1) k [u64.to_int (x % u64.from_int k), ..acc] |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn organ :: n: Int -> List<Int> |
| 47 | { |
| 48 | list.map |
| 49 | (list.range 0 (n - 1)) |
| 50 | fn i |
| 51 | { |
| 52 | if i < n - i |
| 53 | { |
| 54 | i |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | n - i |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Sum of `x * (position % 251 + 1)`: order-sensitive, so a wrong or |
| 64 | /// unstable sort changes it. |
| 65 | fn checksum :: xs: List<Int> -> Int |
| 66 | { |
| 67 | checksum_from xs 0 0 |
| 68 | } |
| 69 | |
| 70 | fn checksum_from :: xs: List<Int> i: Int acc: Int -> Int |
| 71 | { |
| 72 | match xs |
| 73 | { [] -> acc |
| 74 | , [x, ..rest] -> checksum_from rest (i + 1) (acc + x * (i % 251 + 1)) |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /// The checksum of `xs` sorted. |
| 79 | fn sorted_checksum :: xs: List<Int> -> Int |
| 80 | { |
| 81 | checksum (array.to_list (array.sort (array.from_list xs))) |
| 82 | } |
| 83 | |
| 84 | pub fn main :: -> () / <io> |
| 85 | { |
| 86 | // Edge cases. |
| 87 | let empty: List<Int> = [] |
| 88 | io.println (show (array.to_list (array.sort (array.from_list empty)))) |
| 89 | io.println (show (array.to_list (array.sort (array.from_list [2])))) |
| 90 | io.println (show (array.to_list (array.sort (array.from_list [2, 1])))) |
| 91 | // Around the algorithm's thresholds: insertion sort (20), the quicksort |
| 92 | // base case (32), the run-detection minimums beyond. |
| 93 | io.println |
| 94 | ( show |
| 95 | ( list.map |
| 96 | [19, 20, 21, 31, 32, 33, 63, 64, 65, 100, 300] |
| 97 | fn n { sorted_checksum (randoms (prng.seeded n) n []) } |
| 98 | ) |
| 99 | ) |
| 100 | // The detected patterns. |
| 101 | let n = 300 |
| 102 | io.println (show (sorted_checksum (list.range 1 n))) |
| 103 | io.println (show (sorted_checksum (list.reverse (list.range 1 n)))) |
| 104 | io.println (show (sorted_checksum (organ n))) |
| 105 | io.println (show (sorted_checksum (lows (prng.seeded 1) n 3 []))) |
| 106 | io.println (show (sorted_checksum (list.map (list.range 1 n) fn _ { 7 }))) |
| 107 | io.println |
| 108 | ( show |
| 109 | ( sorted_checksum |
| 110 | (list.flat_map (list.range 1 24) fn i { list.range 1 (i % 7) }) |
| 111 | ) |
| 112 | ) |
| 113 | // Strings, and a custom (descending) comparator. |
| 114 | let words = ["pear", "fig", "apple", "fig"] |
| 115 | io.println (show (array.to_list (array.sort (array.from_list words)))) |
| 116 | io.println |
| 117 | ( show |
| 118 | ( array.to_list |
| 119 | ( array.sort_by |
| 120 | (array.from_list [3, 1, 4, 1, 5]) |
| 121 | fn a b { compare b a } |
| 122 | ) |
| 123 | ) |
| 124 | ) |
| 125 | // Stable: the ties on the key keep their original relative order. |
| 126 | let pairs = [(1, "a"), (2, "b"), (1, "c"), (3, "d"), (2, "e"), (1, "f")] |
| 127 | io.println |
| 128 | ( show |
| 129 | ( array.to_list |
| 130 | (array.sort_by (array.from_list pairs) fn x y { compare x.0 y.0 }) |
| 131 | ) |
| 132 | ) |
| 133 | let big_pairs = list.zip (lows (prng.seeded 2) n 5 []) (list.range 1 n) |
| 134 | let sorted_pairs = |
| 135 | array.to_list |
| 136 | (array.sort_by (array.from_list big_pairs) fn x y { compare x.0 y.0 }) |
| 137 | io.println (show (checksum (list.map sorted_pairs fn p { p.0 * 1000 + p.1 }))) |
| 138 | // The comparator may perform effects. |
| 139 | let traced = |
| 140 | array.sort_by |
| 141 | (array.from_list [2, 1, 3]) |
| 142 | fn a b |
| 143 | { |
| 144 | io.println ("cmp " <> show a <> " " <> show b) |
| 145 | compare a b |
| 146 | } |
| 147 | io.println (show (array.to_list traced)) |
| 148 | // A shared array is not touched: the sort works on a copy. |
| 149 | let shared = array.from_list [3, 1, 2] |
| 150 | let sorted = array.sort shared |
| 151 | io.println (show (array.to_list shared) <> " " <> show (array.to_list sorted)) |
| 152 | } |
| 153 | //> [] |
| 154 | //> [2] |
| 155 | //> [1, 2] |
| 156 | //> [271590701347, 302800273131, 371453337698, 703568778846, 860690360577, 810727092261, 2767299647978, 3178009987875, 3006112653713, 7007690599952, 39649304442625] |
| 157 | //> 5650526 |
| 158 | //> 5650526 |
| 159 | //> 2817025 |
| 160 | //> 42130 |
| 161 | //> 229957 |
| 162 | //> 8151 |
| 163 | //> [apple, fig, fig, pear] |
| 164 | //> [5, 4, 3, 1, 1] |
| 165 | //> [(1, a), (1, c), (1, f), (2, b), (2, e), (3, d)] |
| 166 | //> 87995962 |
| 167 | //> cmp 1 2 |
| 168 | //> cmp 3 2 |
| 169 | //> [1, 2, 3] |
| 170 | //> [3, 1, 2] [1, 2, 3] |