1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE TypeFamilies #-}
10 import Data.Vector.Fixed (
22 import qualified Data.Vector.Fixed as V (
27 -- * Low-dimension vector wrappers.
29 -- These wrappers are instances of 'Vector', so they inherit all of
30 -- the userful instances defined above. But, they use fixed
31 -- constructors, so you can pattern match out the individual
35 type instance Dim D1 = N1
36 instance Vector D1 a where
37 inspect (D1 x) (Fun f) = f x
41 type instance Dim D2 = N2
42 instance Vector D2 a where
43 inspect (D2 x y) (Fun f) = f x y
47 type instance Dim D3 = N3
48 instance Vector D3 a where
49 inspect (D3 x y z) (Fun f) = f x y z
52 data D4 a = D4 a a a a
53 type instance Dim D4 = N4
54 instance Vector D4 a where
55 inspect (D4 w x y z) (Fun f) = f w x y z
63 -- >>> let v1 = D2 1 2
67 (!) :: (Vector v a) => v a -> Int -> a
68 (!) v1 idx = (toList v1) !! idx
74 -- >>> let v1 = D3 1 2 3
80 (!?) :: (Vector v a) => v a -> Int -> Maybe a
82 | idx < 0 || idx >= V.length v1 = Nothing
83 | otherwise = Just $ v1 ! idx