Commits

Aleksey Khudyakov committed 67d9019

Add unfoldr

  • Participants
  • Parent commits 7aca5eb

Comments (0)

Files changed (2)

Data/Vector/Fixed.hs

     -- ** Functions
   , replicate
   , replicateM
-  , basis
   , generate
   , generateM
+  , unfoldr
+  , basis
     -- * Modifying vectors
     -- ** Transformations
   , head
 basis = C.vector . C.basis
 
 
+-- | Unfold vector.
+unfoldr :: (Vector v a) => (b -> (a,b)) -> b -> v a
+{-# INLINE unfoldr #-}
+unfoldr f = C.vector . C.unfoldr f
 
 ----------------------------------------------------------------
 

Data/Vector/Fixed/Cont.hs

   , replicateM
   , generate
   , generateM
+  , unfoldr
   , basis
     -- ** Constructors
   , mk1
 
 data T_replicate n = T_replicate
 
+
 -- | Generate vector from function which maps element's index to its value.
 generate :: forall m n a. (Arity n) => (Int -> a) -> ContVecT m n a
 {-# INLINE generate #-}
 
 newtype T_generate n = T_generate Int
 
+-- | Unfold vector.
+unfoldr :: forall m n b a. Arity n => (b -> (a,b)) -> b -> ContVecT m n a
+{-# INLINE unfoldr #-}
+unfoldr f b0 = ContVecT $ \(Fun fun) ->
+  apply (\(T_unfoldr b) -> let (a,b') = f b in (a, T_unfoldr b'))
+        (T_unfoldr b0 :: T_unfoldr b n)
+         fun
+
+newtype T_unfoldr b n = T_unfoldr b
+
+
 -- | Unit vector along Nth axis.
 basis :: forall m n a. (Num a, Arity n) => Int -> ContVecT m n a
 {-# INLINE basis #-}