import std/dict import std/io import std/list fn pairs :: d { show (list.map (dict.to_list d) fn pair { pair.0 <> "=" <> show pair.1 }) } fn check_balanced :: d { // A sorted insert would make an unbalanced tree a list: the AVL // invariant keeps lookups fast. dict.size d } pub fn main :: -> () / { let d = dict.from_list [("pear", 3), ("apple", 1), ("fig", 2)] io.println (pairs d) io.println (show (dict.get d "fig") <> " " <> show (dict.get d "kiwi")) let d2 = dict.insert (dict.remove d "apple") "kiwi" 4 io.println ( show (dict.keys d2) <> " / old still has apple: " <> show (dict.has d "apple") ) let big = list.fold (list.range 1 5000) (dict.empty ()) fn acc i { dict.insert acc i (i * i) } let small = list.fold (list.range 1 4990) big fn acc i { dict.remove acc i } io.println ("size " <> show (check_balanced big) <> " then " <> show (dict.size small)) io.println ("sum of squares " <> show (dict.fold small 0 fn acc k v { acc + v })) io.println (show (dict.values small)) } //> [apple=1, fig=2, pear=3] //> Some(2) None //> [fig, kiwi, pear] / old still has apple: True //> size 5000 then 10 //> sum of squares 249550285 //> [24910081, 24920064, 24930049, 24940036, 24950025, 24960016, 24970009, 24980004, 24990001, 25000000]