Skip to content

Commit 1f14837

Browse files
committed
Add monadic variant for unfoldr
1 parent c057c3a commit 1f14837

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

Data/Vector.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ module Data.Vector (
6060

6161
-- ** Unfolding
6262
unfoldr, unfoldrN,
63+
unfoldrM, unfoldrNM,
6364
constructN, constructrN,
6465

6566
-- ** Enumeration
@@ -633,6 +634,22 @@ unfoldrN :: Int -> (b -> Maybe (a, b)) -> b -> Vector a
633634
{-# INLINE unfoldrN #-}
634635
unfoldrN = G.unfoldrN
635636

637+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
638+
-- generator function to a seed. The generator function yields 'Just'
639+
-- the next element and the new seed or 'Nothing' if there are no more
640+
-- elements.
641+
unfoldrM :: (Monad m) => (b -> m (Maybe (a, b))) -> b -> m (Vector a)
642+
{-# INLINE unfoldrM #-}
643+
unfoldrM = G.unfoldrM
644+
645+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
646+
-- generator function to a seed. The generator function yields 'Just'
647+
-- the next element and the new seed or 'Nothing' if there are no more
648+
-- elements.
649+
unfoldrNM :: (Monad m) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a)
650+
{-# INLINE unfoldrNM #-}
651+
unfoldrNM = G.unfoldrNM
652+
636653
-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
637654
-- generator function to the already constructed part of the vector.
638655
--

Data/Vector/Generic.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Data.Vector.Generic (
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
46+
unfoldrM, unfoldrNM,
4647
constructN, constructrN,
4748

4849
-- ** Enumeration
@@ -551,6 +552,22 @@ unfoldrN :: Vector v a => Int -> (b -> Maybe (a, b)) -> b -> v a
551552
{-# INLINE unfoldrN #-}
552553
unfoldrN n f = unstream . Bundle.unfoldrN n f
553554

555+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
556+
-- generator function to a seed. The generator function yields 'Just'
557+
-- the next element and the new seed or 'Nothing' if there are no more
558+
-- elements.
559+
unfoldrM :: (Monad m, Vector v a) => (b -> m (Maybe (a, b))) -> b -> m (v a)
560+
{-# INLINE unfoldrM #-}
561+
unfoldrM f = unstreamM . MBundle.unfoldrM f
562+
563+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
564+
-- generator function to a seed. The generator function yields 'Just'
565+
-- the next element and the new seed or 'Nothing' if there are no more
566+
-- elements.
567+
unfoldrNM :: (Monad m, Vector v a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (v a)
568+
{-# INLINE unfoldrNM #-}
569+
unfoldrNM n f = unstreamM . MBundle.unfoldrNM n f
570+
554571
-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
555572
-- generator function to the already constructed part of the vector.
556573
--

Data/Vector/Primitive.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Data.Vector.Primitive (
4646

4747
-- ** Unfolding
4848
unfoldr, unfoldrN,
49+
unfoldrM, unfoldrNM,
4950
constructN, constructrN,
5051

5152
-- ** Enumeration
@@ -516,6 +517,22 @@ unfoldrN :: Prim a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
516517
{-# INLINE unfoldrN #-}
517518
unfoldrN = G.unfoldrN
518519

520+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
521+
-- generator function to a seed. The generator function yields 'Just'
522+
-- the next element and the new seed or 'Nothing' if there are no more
523+
-- elements.
524+
unfoldrM :: (Monad m, Prim a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a)
525+
{-# INLINE unfoldrM #-}
526+
unfoldrM = G.unfoldrM
527+
528+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
529+
-- generator function to a seed. The generator function yields 'Just'
530+
-- the next element and the new seed or 'Nothing' if there are no more
531+
-- elements.
532+
unfoldrNM :: (Monad m, Prim a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a)
533+
{-# INLINE unfoldrNM #-}
534+
unfoldrNM = G.unfoldrNM
535+
519536
-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
520537
-- generator function to the already constructed part of the vector.
521538
--

Data/Vector/Storable.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module Data.Vector.Storable (
4343

4444
-- ** Unfolding
4545
unfoldr, unfoldrN,
46+
unfoldrM, unfoldrNM,
4647
constructN, constructrN,
4748

4849
-- ** Enumeration
@@ -526,6 +527,22 @@ unfoldrN :: Storable a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
526527
{-# INLINE unfoldrN #-}
527528
unfoldrN = G.unfoldrN
528529

530+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
531+
-- generator function to a seed. The generator function yields 'Just'
532+
-- the next element and the new seed or 'Nothing' if there are no more
533+
-- elements.
534+
unfoldrM :: (Monad m, Storable a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a)
535+
{-# INLINE unfoldrM #-}
536+
unfoldrM = G.unfoldrM
537+
538+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
539+
-- generator function to a seed. The generator function yields 'Just'
540+
-- the next element and the new seed or 'Nothing' if there are no more
541+
-- elements.
542+
unfoldrNM :: (Monad m, Storable a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a)
543+
{-# INLINE unfoldrNM #-}
544+
unfoldrNM = G.unfoldrNM
545+
529546
-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
530547
-- generator function to the already constructed part of the vector.
531548
--

Data/Vector/Unboxed.hs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ module Data.Vector.Unboxed (
6666

6767
-- ** Unfolding
6868
unfoldr, unfoldrN,
69+
unfoldrM, unfoldrNM,
6970
constructN, constructrN,
7071

7172
-- ** Enumeration
@@ -495,6 +496,22 @@ unfoldrN :: Unbox a => Int -> (b -> Maybe (a, b)) -> b -> Vector a
495496
{-# INLINE unfoldrN #-}
496497
unfoldrN = G.unfoldrN
497498

499+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
500+
-- generator function to a seed. The generator function yields 'Just'
501+
-- the next element and the new seed or 'Nothing' if there are no more
502+
-- elements.
503+
unfoldrM :: (Monad m, Unbox a) => (b -> m (Maybe (a, b))) -> b -> m (Vector a)
504+
{-# INLINE unfoldrM #-}
505+
unfoldrM = G.unfoldrM
506+
507+
-- | /O(n)/ Construct a vector by repeatedly applying the monadic
508+
-- generator function to a seed. The generator function yields 'Just'
509+
-- the next element and the new seed or 'Nothing' if there are no more
510+
-- elements.
511+
unfoldrNM :: (Monad m, Unbox a) => Int -> (b -> m (Maybe (a, b))) -> b -> m (Vector a)
512+
{-# INLINE unfoldrNM #-}
513+
unfoldrNM = G.unfoldrNM
514+
498515
-- | /O(n)/ Construct a vector with @n@ elements by repeatedly applying the
499516
-- generator function to the already constructed part of the vector.
500517
--

0 commit comments

Comments
 (0)