Added iteraten and generaten functions
[matthijs/master-project/support/tfvec.git] / Data / Param / TFVec.hs
index 50b2fda258b44de6c285aeacae5889aeeddd5ab9..fd620e74887df2bb2349170252573c181e5cfa4b 100644 (file)
@@ -35,7 +35,6 @@ module Data.Param.TFVec
   , take
   , drop
   , select
---  , group
   , (<+)
   , (++)
   , map
@@ -51,7 +50,9 @@ module Data.Param.TFVec
   , concat
   , reverse
   , iterate
+  , iteraten
   , generate
+  , generaten
   , copy
   , copyn
   ) where
@@ -175,13 +176,6 @@ select f s n = liftV (select' f' s' n')
           | otherwise = []
         selectFirst0 _ 0 [] = []
 
--- group :: PositiveT n => n -> TFVec s a -> TFVec (Div s n) (TFVec n a)
--- group n = liftV (group' (fromIntegerT n))
---   where group' :: Int -> [a] -> [TFVec s a]
---         group' n xs = case splitAtM n xs of
---                         Nothing -> []
---                         Just (ls, rs) -> TFVec ls : group' n rs
-
 (<+) :: TFVec s a -> a -> TFVec (Succ s) a
 (<+) (TFVec xs) x = TFVec (xs P.++ [x])
 
@@ -217,11 +211,18 @@ shiftr :: (PositiveT s, NaturalT n, n ~ Pred s, s ~ Succ n) =>
           TFVec s a -> a -> TFVec s a
 shiftr xs x = tail xs <+ x
   
-rotl :: (PositiveT s, s ~ Succ (Pred s)) => TFVec s a -> TFVec s a
-rotl xs = last xs +> init xs
-
-rotr :: (PositiveT s, s ~ Succ (Pred s)) => TFVec s a -> TFVec s a
-rotr xs = tail xs <+ head xs
+rotl :: forall s a . NaturalT s => TFVec s a -> TFVec s a
+rotl = liftV rotl'
+  where vlen = fromIntegerT (undefined :: s)
+        rotl' [] = []
+        rotl' xs = let (i,[l]) = splitAt (vlen - 1) xs
+                   in l : i 
+
+rotr :: NaturalT s => TFVec s a -> TFVec s a
+rotr = liftV rotr'
+  where
+    rotr' [] = []
+    rotr' (x:xs) = xs P.++ [x] 
 
 concat :: TFVec s1 (TFVec s2 a) -> TFVec (s1 :*: s2) a
 concat = liftV (P.foldr ((P.++).unTFVec) [])
@@ -229,17 +230,23 @@ concat = liftV (P.foldr ((P.++).unTFVec) [])
 reverse :: TFVec s a -> TFVec s a
 reverse = liftV P.reverse
 
-iterate :: NaturalT s => s -> (a -> a) -> a -> TFVec s a
-iterate s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.iterate f x)
+iterate :: NaturalT s => (a -> a) -> a -> TFVec s a
+iterate = iteraten (undefined :: s)
+
+iteraten :: NaturalT s => s -> (a -> a) -> a -> TFVec s a
+iteraten s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.iterate f x)
+
+generate :: NaturalT s => (a -> a) -> a -> TFVec s a
+generate = generaten (undefined :: s)
 
-generate :: NaturalT s => s -> (a -> a) -> a -> TFVec s a
-generate s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.tail $ P.iterate f x)
+generaten :: NaturalT s => s -> (a -> a) -> a -> TFVec s a
+generaten s f x = let s' = fromIntegerT s in TFVec (P.take s' $ P.tail $ P.iterate f x)
 
 copy :: NaturalT s => a -> TFVec s a
 copy x = copyn (undefined :: s) x
 
 copyn :: NaturalT s => s -> a -> TFVec s a
-copyn s x = iterate s id x
+copyn s x = iteraten s id x
 
 -- =============
 -- = Instances =