on master help sign in
Add unfoldrM for serial streams

This combinator does not require the MonadAsync constraint, therefore, it can be used in mondads not based on IO. fixes #326

commit2ab0d8f42b8bb6b38b480f7be4aa204c0b65d8ed authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 11 Dec 2019, 08:02 (7 years ago) parent9ea72e9
2 files changed+39−5
src/Streamly/Internal/Data/Stream/Serial.hs+38−4 src/Streamly/Internal/Prelude.hs+1−1
src/Streamly/Internal/Data/Stream/Serial.hs+38−4
@@ -22,25 +22,30 @@ module Streamly.Internal.Data.Stream.Serial
2222 (
2323 -- * Serial appending stream
2424 SerialT
25− , StreamT -- deprecated
2625 , Serial
2726 , K.serial
2827 , serially
2928
3029 -- * Serial interleaving stream
3130 , WSerialT
32− , InterleavedT -- deprecated
3331 , WSerial
3432 , wSerial
3533 , wSerialFst
3634 , wSerialMin
37− , (<=>) -- deprecated
3835 , wSerially
39− , interleaving -- deprecated
36+
37+ -- * Construction
38+ , unfoldrM
4039
4140 -- * Transformation
4241 , map
4342 , mapM
43+
44+ -- * Deprecated
45+ , StreamT
46+ , InterleavedT
47+ , (<=>)
48+ , interleaving
4449 )
4550where
4651
@@ -397,3 +402,32 @@ LIST_INSTANCES(WSerialT)
397402NFDATA1_INSTANCE(WSerialT)
398403FOLDABLE_INSTANCE(WSerialT)
399404TRAVERSABLE_INSTANCE(WSerialT)
405+
406+------------------------------------------------------------------------------
407+-- Construction
408+------------------------------------------------------------------------------
409+
410+-- | Build a stream by unfolding a /monadic/ step function starting from a
411+-- seed. The step function returns the next element in the stream and the next
412+-- seed value. When it is done it returns 'Nothing' and the stream ends. For
413+-- example,
414+--
415+-- @
416+-- let f b =
417+-- if b > 3
418+-- then return Nothing
419+-- else print b >> return (Just (b, b + 1))
420+-- in drain $ unfoldrM f 0
421+-- @
422+-- @
423+-- 0
424+-- 1
425+-- 2
426+-- 3
427+-- @
428+--
429+-- /Internal/
430+--
431+{-# INLINE unfoldrM #-}
432+unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
433+unfoldrM step seed = D.fromStreamD (D.unfoldrM step seed)
src/Streamly/Internal/Prelude.hs+1−1
@@ -569,7 +569,7 @@ unfoldrM = K.unfoldrM
569569{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
570570{-# INLINE_EARLY unfoldrMSerial #-}
571571unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
572−unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
572+unfoldrMSerial = Serial.unfoldrM
573573
574574-- | Convert an 'Unfold' into a stream by supplying it an input seed.
575575--