tobogganSe connecter

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.

44 lignes · 1320 octetsBrut · Historique
1import std/dict
2import std/io
3import std/list
4
5fn pairs :: d
6{
7 show (list.map (dict.to_list d) fn pair { pair.0 <> "=" <> show pair.1 })
8}
9
10fn 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
17pub 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]