Skip to content

Commit c057c3a

Browse files
committed
Add monadic variant of iterate
1 parent 1cada63 commit c057c3a

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

Data/Vector.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module Data.Vector (
5656
empty, singleton, replicate, generate, iterateN,
5757

5858
-- ** Monadic initialisation
59-
replicateM, generateM, create, createT,
59+
replicateM, generateM, iterateNM, create, createT,
6060

6161
-- ** Unfolding
6262
unfoldr, unfoldrN,
@@ -726,6 +726,11 @@ generateM :: Monad m => Int -> (Int -> m a) -> m (Vector a)
726726
{-# INLINE generateM #-}
727727
generateM = G.generateM
728728

729+
-- | /O(n)/ Apply monadic function n times to value. Zeroth element is original value.
730+
iterateNM :: Monad m => Int -> (a -> m a) -> a -> m (Vector a)
731+
{-# INLINE iterateNM #-}
732+
iterateNM = G.iterateNM
733+
729734
-- | Execute the monadic action and freeze the resulting vector.
730735
--
731736
-- @

Data/Vector/Generic.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Data.Vector.Generic (
3939
empty, singleton, replicate, generate, iterateN,
4040

4141
-- ** Monadic initialisation
42-
replicateM, generateM, create, createT,
42+
replicateM, generateM, iterateNM, create, createT,
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
@@ -711,6 +711,11 @@ generateM :: (Monad m, Vector v a) => Int -> (Int -> m a) -> m (v a)
711711
{-# INLINE generateM #-}
712712
generateM n f = unstreamM (MBundle.generateM n f)
713713

714+
-- | /O(n)/ Apply monadic function n times to value. Zeroth element is original value.
715+
iterateNM :: (Monad m, Vector v a) => Int -> (a -> m a) -> a -> m (v a)
716+
{-# INLINE iterateNM #-}
717+
iterateNM n f x = unstreamM (MBundle.iterateNM n f x)
718+
714719
-- | Execute the monadic action and freeze the resulting vector.
715720
--
716721
-- @

Data/Vector/Primitive.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Data.Vector.Primitive (
4242
empty, singleton, replicate, generate, iterateN,
4343

4444
-- ** Monadic initialisation
45-
replicateM, generateM, create, createT,
45+
replicateM, generateM, iterateNM, create, createT,
4646

4747
-- ** Unfolding
4848
unfoldr, unfoldrN,
@@ -609,6 +609,11 @@ generateM :: (Monad m, Prim a) => Int -> (Int -> m a) -> m (Vector a)
609609
{-# INLINE generateM #-}
610610
generateM = G.generateM
611611

612+
-- | /O(n)/ Apply monadic function n times to value. Zeroth element is original value.
613+
iterateNM :: (Monad m, Prim a) => Int -> (a -> m a) -> a -> m (Vector a)
614+
{-# INLINE iterateNM #-}
615+
iterateNM = G.iterateNM
616+
612617
-- | Execute the monadic action and freeze the resulting vector.
613618
--
614619
-- @

Data/Vector/Storable.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ module Data.Vector.Storable (
3939
empty, singleton, replicate, generate, iterateN,
4040

4141
-- ** Monadic initialisation
42-
replicateM, generateM, create, createT,
42+
replicateM, generateM, iterateNM, create, createT,
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
@@ -619,6 +619,11 @@ generateM :: (Monad m, Storable a) => Int -> (Int -> m a) -> m (Vector a)
619619
{-# INLINE generateM #-}
620620
generateM = G.generateM
621621

622+
-- | /O(n)/ Apply monadic function n times to value. Zeroth element is original value.
623+
iterateNM :: (Monad m, Storable a) => Int -> (a -> m a) -> a -> m (Vector a)
624+
{-# INLINE iterateNM #-}
625+
iterateNM = G.iterateNM
626+
622627
-- | Execute the monadic action and freeze the resulting vector.
623628
--
624629
-- @

Data/Vector/Unboxed.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module Data.Vector.Unboxed (
6262
empty, singleton, replicate, generate, iterateN,
6363

6464
-- ** Monadic initialisation
65-
replicateM, generateM, create, createT,
65+
replicateM, generateM, iterateNM, create, createT,
6666

6767
-- ** Unfolding
6868
unfoldr, unfoldrN,
@@ -588,6 +588,11 @@ generateM :: (Monad m, Unbox a) => Int -> (Int -> m a) -> m (Vector a)
588588
{-# INLINE generateM #-}
589589
generateM = G.generateM
590590

591+
-- | /O(n)/ Apply monadic function n times to value. Zeroth element is original value.
592+
iterateNM :: (Monad m, Unbox a) => Int -> (a -> m a) -> a -> m (Vector a)
593+
{-# INLINE iterateNM #-}
594+
iterateNM = G.iterateNM
595+
591596
-- | Execute the monadic action and freeze the resulting vector.
592597
--
593598
-- @

0 commit comments

Comments
 (0)