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#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | import std/dict |
| 2 | import std/io |
| 3 | import std/list |
| 4 | |
| 5 | fn pairs :: d |
| 6 | { |
| 7 | show (list.map (dict.to_list d) fn pair { pair.0 <> "=" <> show pair.1 }) |
| 8 | } |
| 9 | |
| 10 | fn check_balanced :: d |
| 11 | { |
| 12 | // A sorted insert would make an unbalanced tree a list: the AVL |
| 13 | // invariant keeps lookups fast. |
| 14 | dict.size d |
| 15 | } |
| 16 | |
| 17 | pub fn main :: -> () / <io> |
| 18 | { |
| 19 | let d = dict.from_list [("pear", 3), ("apple", 1), ("fig", 2)] |
| 20 | io.println (pairs d) |
| 21 | io.println (show (dict.get d "fig") <> " " <> show (dict.get d "kiwi")) |
| 22 | let d2 = dict.insert (dict.remove d "apple") "kiwi" 4 |
| 23 | io.println |
| 24 | ( show (dict.keys d2) <> " / old still has apple: " |
| 25 | <> show (dict.has d "apple") |
| 26 | ) |
| 27 | let big = |
| 28 | list.fold |
| 29 | (list.range 1 5000) |
| 30 | (dict.empty ()) |
| 31 | fn acc i { dict.insert acc i (i * i) } |
| 32 | let small = list.fold (list.range 1 4990) big fn acc i { dict.remove acc i } |
| 33 | io.println |
| 34 | ("size " <> show (check_balanced big) <> " then " <> show (dict.size small)) |
| 35 | io.println |
| 36 | ("sum of squares " <> show (dict.fold small 0 fn acc k v { acc + v })) |
| 37 | io.println (show (dict.values small)) |
| 38 | } |
| 39 | //> [apple=1, fig=2, pear=3] |
| 40 | //> Some(2) None |
| 41 | //> [fig, kiwi, pear] / old still has apple: True |
| 42 | //> size 5000 then 10 |
| 43 | //> sum of squares 249550285 |
| 44 | //> [24910081, 24920064, 24930049, 24940036, 24950025, 24960016, 24970009, 24980004, 24990001, 25000000] |