1module DictBench where 2 3import Dict 4import Random 5import Debug exposing (crash) 6 7 8 9randomDict 10 : Random.Generator a 11 -> Int 12 -> Random.Seed 13 -> Dict.Dict Int a 14randomDict gen n initSeed = 15 let 16 f n (seed, accum) = 17 let 18 (nextToAdd, newSeed) = Random.generate gen seed 19 newAccum = Dict.insert n nextToAdd accum 20 in (newSeed, newAccum) 21 in 22 snd <| List.foldl f (initSeed, Dict.empty) [1 .. n] 23 24meanAndStddev 25 : ((Int -> Float -> (Float, Float) -> (Float, Float)) -> (Float, Float) -> Dict.Dict Int Float -> (Float, Float)) 26 -> Dict.Dict Int Float 27 -> (Float, Float) 28meanAndStddev foldFn ourDict = 29 let 30 --ourDict = randomDict n (Random.initialSeed 23) 31 (theSum, sumSq) = foldFn (\_ x (sum, sumSq) -> (x + sum, (x^2) + sumSq) ) (0,0) ourDict 32 in 33 (theSum, sqrt(theSum*theSum - sumSq)) 34 35 36foldrMeanStddev = meanAndStddev Dict.foldr 37 38 39foldlMeanStddev = meanAndStddev Dict.foldl 40 41 42updateNRandomIndices 43 : (Float -> Float) 44 -> List Int 45 -> Dict.Dict Int Float 46 -> Dict.Dict Int Float 47updateNRandomIndices f indexList startList = 48 List.foldr 49 (\ i oldList -> updateN i f oldList) 50 startList indexList 51 52 53randomLookups : List Int -> Dict.Dict Int Float -> List (Maybe Float) 54randomLookups indices arr = 55 List.map (\i -> Dict.get i arr) indices 56 57 58 59updateN : Int -> (a -> a) -> Dict.Dict Int a -> Dict.Dict Int a 60updateN n f arr = 61 case Dict.get n arr of 62 Just x -> Dict.insert n (f x) arr 63 _ -> crash "Dict out of bounds"