1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
10 import Data.Vector.Fixed (
24 import qualified Data.Vector.Fixed as V (
29 -- * Low-dimension vector wrappers.
31 -- These wrappers are instances of 'Vector', so they inherit all of
32 -- the userful instances defined above. But, they use fixed
33 -- constructors, so you can pattern match out the individual
36 data D1 a = D1 a deriving (Show, Eq)
37 type instance Dim D1 = N1
38 instance Vector D1 a where
39 inspect (D1 x) (Fun f) = f x
42 data D2 a = D2 a a deriving (Show, Eq)
43 type instance Dim D2 = N2
44 instance Vector D2 a where
45 inspect (D2 x y) (Fun f) = f x y
48 data D3 a = D3 a a a deriving (Show, Eq)
49 type instance Dim D3 = N3
50 instance Vector D3 a where
51 inspect (D3 x y z) (Fun f) = f x y z
54 data D4 a = D4 a a a a deriving (Show, Eq)
55 type instance Dim D4 = N4
56 instance Vector D4 a where
57 inspect (D4 w x y z) (Fun f) = f w x y z
65 -- >>> let v1 = D2 1 2
69 (!) :: (Vector v a) => v a -> Int -> a
70 (!) v1 idx = (toList v1) !! idx
76 -- >>> let v1 = D3 1 2 3
82 (!?) :: (Vector v a) => v a -> Int -> Maybe a
84 | idx < 0 || idx >= V.length v1 = Nothing
85 | otherwise = Just $ v1 ! idx
88 -- | Remove an element of the given vector.
92 -- >>> let b = D3 1 2 3
93 -- >>> delete b 1 :: D2 Int
96 delete :: (Vector v a, Vector w a, Dim v ~ S (Dim w)) => v a -> Int -> w a
98 fromList $ (lhalf ++ rhalf')
100 (lhalf, rhalf) = splitAt idx (toList v1)