never executed always true always false
1 module HelVM.HelIO.Containers.LLInsertDef where
2
3 import Data.Default
4 import Data.Sequence ((|>))
5
6 import qualified Data.IntMap as IntMap
7 import qualified Data.Sequence as Seq
8
9 -- | Insert a new element
10 naturalInsertDef :: InsertDef full item => Natural -> item -> full -> full
11 naturalInsertDef = insertDef <$> fromIntegral
12
13 -- | Type Class
14 class InsertDef full item | full -> item where
15 insertDef :: Int -> item -> full -> full
16
17 instance Default a => InsertDef [a] a where
18 insertDef 0 e [] = [e]
19 insertDef 0 e (_:xs) = e : xs
20 insertDef i e [] = def : insertDef (i-1) e []
21 insertDef i e (x:xs) = x : insertDef (i-1) e xs
22
23 instance Default a => InsertDef (Seq a) a where
24 insertDef i e c = (check <$> Seq.length) c where
25 check l
26 | i < l = Seq.update i e c
27 | otherwise = c <> Seq.replicate (i - l) def |> e
28
29 instance InsertDef (IntMap a) a where
30 insertDef = IntMap.insert