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