on master help sign in
Add minimal docs and to multiple functions in Internal.Data.Unfold
cd40d90adithyaov
src/Streamly/Internal/Data/Unfold.hs+44−1
@@ -469,6 +469,12 @@ fromListM = Unfold step inject
469 step (x:xs) = x >>= \r -> return $ Yield r xs
469 step (x:xs) = x >>= \r -> return $ Yield r xs
470 step [] = return Stop
470 step [] = return Stop
471
471
472+-- | Build a stream by unfolding a /monadic/ step function starting from a seed.
473+-- The step function returns the next element in the stream and the next seed
474+-- value. When it is done it returns 'Nothing' and the stream ends.
475+--
476+-- /Internal/
477+--
472{-# INLINE unfoldrM #-}
478{-# INLINE unfoldrM #-}
473unfoldrM :: Monad m => (a -> m (Maybe (b, a))) -> Unfold m a b
479unfoldrM :: Monad m => (a -> m (Maybe (b, a))) -> Unfold m a b
474unfoldrM next = Unfold step return
480unfoldrM next = Unfold step return
@@ -486,6 +492,8 @@ unfoldrM next = Unfold step return
486
492
487-- | Generates a stream replicating the seed @n@ times.
493-- | Generates a stream replicating the seed @n@ times.
488--
494--
495+-- /Internal/
496+--
489{-# INLINE replicateM #-}
497{-# INLINE replicateM #-}
490replicateM :: Monad m => Int -> Unfold m (m a) a
498replicateM :: Monad m => Int -> Unfold m (m a) a
491replicateM n = Unfold step inject
499replicateM n = Unfold step inject
@@ -504,6 +512,8 @@ replicateM n = Unfold step inject
504
512
505-- | Generates an infinite stream repeating the seed.
513-- | Generates an infinite stream repeating the seed.
506--
514--
515+-- /Internal/
516+--
507{-# INLINE repeatM #-}
517{-# INLINE repeatM #-}
508repeatM :: Monad m => Unfold m (m a) a
518repeatM :: Monad m => Unfold m (m a) a
509repeatM = Unfold step return
519repeatM = Unfold step return
@@ -514,6 +524,8 @@ repeatM = Unfold step return
514-- | Generates an infinite stream starting with the given seed and applying the
524-- | Generates an infinite stream starting with the given seed and applying the
515-- given function repeteadly.
525-- given function repeteadly.
516--
526--
527+-- /Internal/
528+--
517{-# INLINE iterateM #-}
529{-# INLINE iterateM #-}
518iterateM :: Monad m => (a -> m a) -> Unfold m a a
530iterateM :: Monad m => (a -> m a) -> Unfold m a a
519iterateM f = Unfold step f
531iterateM f = Unfold step f
@@ -526,6 +538,8 @@ iterateM f = Unfold step f
526-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
538-- | @fromIndicesM gen@ generates an infinite stream of values using @gen@
527-- starting from the seed.
539-- starting from the seed.
528--
540--
541+-- /Internal/
542+--
529{-# INLINE_NORMAL fromIndicesM #-}
543{-# INLINE_NORMAL fromIndicesM #-}
530fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
544fromIndicesM :: Monad m => (Int -> m a) -> Unfold m Int a
531fromIndicesM gen = Unfold step return
545fromIndicesM gen = Unfold step return
@@ -591,6 +605,10 @@ filterM f (Unfold step1 inject1) = Unfold step inject1
591filter :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
605filter :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
592filter f = filterM (return . f)
606filter f = filterM (return . f)
593
607
608+-- | @drop n unf@ drops @n@ elements from the stream generated by @unf@.
609+--
610+-- /Internal/
611+--
594{-# INLINE_NORMAL drop #-}
612{-# INLINE_NORMAL drop #-}
595drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
613drop :: Monad m => Int -> Unfold m a b -> Unfold m a b
596drop n (Unfold step inject) = Unfold step' inject'
614drop n (Unfold step inject) = Unfold step' inject'
@@ -618,6 +636,11 @@ drop n (Unfold step inject) = Unfold step' inject'
618 Skip s -> Skip (s, 0)
636 Skip s -> Skip (s, 0)
619 Stop -> Stop
637 Stop -> Stop
620
638
639+-- | @dropWhileM f unf@ drops elements from the stream generated by @unf@ while
640+-- the condition holds true. The condition function @f@ is /monadic/ in nature.
641+--
642+-- /Internal/
643+--
621{-# INLINE_NORMAL dropWhileM #-}
644{-# INLINE_NORMAL dropWhileM #-}
622dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
645dropWhileM :: Monad m => (b -> m Bool) -> Unfold m a b -> Unfold m a b
623dropWhileM f (Unfold step inject) = Unfold step' inject'
646dropWhileM f (Unfold step inject) = Unfold step' inject'
@@ -648,6 +671,10 @@ dropWhileM f (Unfold step inject) = Unfold step' inject'
648 Skip s -> Skip (Right s)
671 Skip s -> Skip (Right s)
649 Stop -> Stop
672 Stop -> Stop
650
673
674+-- | Similar to 'dropWhileM' but with a pure condition function.
675+--
676+-- /Internal/
677+--
651{-# INLINE dropWhile #-}
678{-# INLINE dropWhile #-}
652dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
679dropWhile :: Monad m => (b -> Bool) -> Unfold m a b -> Unfold m a b
653dropWhile f = dropWhileM (return . f)
680dropWhile f = dropWhileM (return . f)
@@ -677,6 +704,11 @@ enumerateFromToIntegral to =
677enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
704enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
678enumerateFromIntegral = enumerateFromToIntegral maxBound
705enumerateFromIntegral = enumerateFromToIntegral maxBound
679
706
707+-- | Generate an infinite stream starting from the seed with increments of the
708+-- given stride.
709+--
710+-- /Internal/
711+--
680{-# INLINE enumerateFromStepNum #-}
712{-# INLINE enumerateFromStepNum #-}
681enumerateFromStepNum :: (Monad m, Num a) => a -> Unfold m a a
713enumerateFromStepNum :: (Monad m, Num a) => a -> Unfold m a a
682enumerateFromStepNum stride = Unfold step return
714enumerateFromStepNum stride = Unfold step return
@@ -684,10 +716,16 @@ enumerateFromStepNum stride = Unfold step return
684 {-# INLINE_LATE step #-}
716 {-# INLINE_LATE step #-}
685 step !s = return $ (Yield $! s) $! (s + stride)
717 step !s = return $ (Yield $! s) $! (s + stride)
686
718
719+-- | @numFrom = enumerateFromStepNum 1@
720+--
721+-- /Internal/
722+--
687{-# INLINE_NORMAL numFrom #-}
723{-# INLINE_NORMAL numFrom #-}
688numFrom :: (Monad m, Num a) => Unfold m a a
724numFrom :: (Monad m, Num a) => Unfold m a a
689numFrom = enumerateFromStepNum 1
725numFrom = enumerateFromStepNum 1
690
726
727+-- | /Internal/
728+--
691{-# INLINE_NORMAL enumerateFromToFractional #-}
729{-# INLINE_NORMAL enumerateFromToFractional #-}
692enumerateFromToFractional :: (Monad m, Fractional a, Ord a) => a -> Unfold m a a
730enumerateFromToFractional :: (Monad m, Fractional a, Ord a) => a -> Unfold m a a
693enumerateFromToFractional to =
731enumerateFromToFractional to =
@@ -703,6 +741,8 @@ data FromSVarState t m a =
703 | FromSVarLoop (SVar t m a) [ChildEvent a]
741 | FromSVarLoop (SVar t m a) [ChildEvent a]
704 | FromSVarDone (SVar t m a)
742 | FromSVarDone (SVar t m a)
705
743
744+-- | /Internal/
745+--
706{-# INLINE_NORMAL fromSVar #-}
746{-# INLINE_NORMAL fromSVar #-}
707fromSVar :: MonadAsync m => Unfold m (SVar t m a) a
747fromSVar :: MonadAsync m => Unfold m (SVar t m a) a
708fromSVar = Unfold step (return . FromSVarInit)
748fromSVar = Unfold step (return . FromSVarInit)
@@ -782,7 +822,10 @@ fromSVar = Unfold step (return . FromSVarInit)
782-- Process events received by a fold consumer from a stream producer
822-- Process events received by a fold consumer from a stream producer
783-------------------------------------------------------------------------------
823-------------------------------------------------------------------------------
784
824
785−-- XXX Why don't we do the same thing for "FromSVarInit" that we did in "fromSVar"?
825+-- XXX Have an error for "FromSVarInit" instead of undefined. The error can
826+-- redirect the user to report the failure to the developers.
827+-- | /Internal/
828+--
786{-# INLINE_NORMAL fromProducer #-}
829{-# INLINE_NORMAL fromProducer #-}
787fromProducer :: MonadAsync m => Unfold m (SVar t m a) a
830fromProducer :: MonadAsync m => Unfold m (SVar t m a) a
788fromProducer = Unfold step (return . FromSVarRead)
831fromProducer = Unfold step (return . FromSVarRead)