1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
10 import Data.Vector.Fixed (
25 import qualified Data.Vector.Fixed as V (
28 import Data.Vector.Fixed.Boxed
34 -- * Low-dimension vector wrappers.
36 -- These wrappers are instances of 'Vector', so they inherit all of
37 -- the userful instances defined above. But, they use fixed
38 -- constructors, so you can pattern match out the individual
41 data D1 a = D1 a deriving (Show, Eq)
42 type instance Dim D1 = N1
43 instance Vector D1 a where
44 inspect (D1 x) (Fun f) = f x
47 data D2 a = D2 a a deriving (Show, Eq)
48 type instance Dim D2 = N2
49 instance Vector D2 a where
50 inspect (D2 x y) (Fun f) = f x y
53 data D3 a = D3 a a a deriving (Show, Eq)
54 type instance Dim D3 = N3
55 instance Vector D3 a where
56 inspect (D3 x y z) (Fun f) = f x y z
59 data D4 a = D4 a a a a deriving (Show, Eq)
60 type instance Dim D4 = N4
61 instance Vector D4 a where
62 inspect (D4 w x y z) (Fun f) = f w x y z
70 -- >>> let v1 = D2 1 2
74 (!) :: (Vector v a) => v a -> Int -> a
75 (!) v1 idx = (toList v1) !! idx
81 -- >>> let v1 = D3 1 2 3
87 (!?) :: (Vector v a) => v a -> Int -> Maybe a
89 | idx < 0 || idx >= V.length v1 = Nothing
90 | otherwise = Just $ v1 ! idx
93 -- | Remove an element of the given vector.
97 -- >>> let b = D3 1 2 3
98 -- >>> delete b 1 :: D2 Int
101 delete :: (Vector v a, Vector w a, Dim v ~ S (Dim w)) => v a -> Int -> w a
103 fromList $ (lhalf ++ rhalf')
105 (lhalf, rhalf) = splitAt idx (toList v1)