Write an IntMap module to clean up indexed access code.
This commit is contained in:
parent
e3ccab2c82
commit
72e134ee79
30
src/CacheSim/IntMap.elm
Normal file
30
src/CacheSim/IntMap.elm
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
module CacheSim.IntMap exposing (..)
|
||||||
|
|
||||||
|
type alias IntMap a = List a
|
||||||
|
|
||||||
|
intMapGet : Int -> IntMap a -> Maybe a
|
||||||
|
intMapGet i m =
|
||||||
|
case (i, m) of
|
||||||
|
(_, []) -> Nothing
|
||||||
|
(0, (x::xs)) -> Just x
|
||||||
|
(n, (x::xs)) -> intMapGet (n-1) xs
|
||||||
|
|
||||||
|
intMapUpdate : Int -> (a -> a) -> IntMap a -> IntMap a
|
||||||
|
intMapUpdate i f m =
|
||||||
|
case (i, m) of
|
||||||
|
(_, []) -> []
|
||||||
|
(0, (x::xs)) -> (f x)::xs
|
||||||
|
(n, (x::xs)) -> intMapUpdate (n-1) f xs
|
||||||
|
|
||||||
|
intMapPut : Int -> a -> IntMap a -> IntMap a
|
||||||
|
intMapPut i v = intMapUpdate i (\_ -> v)
|
||||||
|
|
||||||
|
intMapFind : (a -> Bool) -> IntMap a -> Maybe Int
|
||||||
|
intMapFind f m =
|
||||||
|
let
|
||||||
|
findHelper n l =
|
||||||
|
case l of
|
||||||
|
[] -> Nothing
|
||||||
|
(x::xs) -> if f x then Just n else findHelper (n - 1) xs
|
||||||
|
in
|
||||||
|
findHelper 0 m
|
Loading…
Reference in New Issue
Block a user