on master help sign in
Add unfoldrM for serial streams
2ab0d8fHarendra Kumar
src/Streamly/Internal/Data/Stream/Serial.hs+38−4
@@ -22,25 +22,30 @@ module Streamly.Internal.Data.Stream.Serial
22 (
22 (
23 -- * Serial appending stream
23 -- * Serial appending stream
24 SerialT
24 SerialT
25− , StreamT -- deprecated
26 , Serial
25 , Serial
27 , K.serial
26 , K.serial
28 , serially
27 , serially
29
28
30 -- * Serial interleaving stream
29 -- * Serial interleaving stream
31 , WSerialT
30 , WSerialT
32− , InterleavedT -- deprecated
33 , WSerial
31 , WSerial
34 , wSerial
32 , wSerial
35 , wSerialFst
33 , wSerialFst
36 , wSerialMin
34 , wSerialMin
37− , (<=>) -- deprecated
38 , wSerially
35 , wSerially
39− , interleaving -- deprecated
36+
37+ -- * Construction
38+ , unfoldrM
40
39
41 -- * Transformation
40 -- * Transformation
42 , map
41 , map
43 , mapM
42 , mapM
43+
44+ -- * Deprecated
45+ , StreamT
46+ , InterleavedT
47+ , (<=>)
48+ , interleaving
44 )
49 )
45where
50where
46
51
@@ -397,3 +402,32 @@ LIST_INSTANCES(WSerialT)
397NFDATA1_INSTANCE(WSerialT)
402NFDATA1_INSTANCE(WSerialT)
398FOLDABLE_INSTANCE(WSerialT)
403FOLDABLE_INSTANCE(WSerialT)
399TRAVERSABLE_INSTANCE(WSerialT)
404TRAVERSABLE_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
569{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
569{-# RULES "unfoldrM serial" unfoldrM = unfoldrMSerial #-}
570{-# INLINE_EARLY unfoldrMSerial #-}
570{-# INLINE_EARLY unfoldrMSerial #-}
571unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
571unfoldrMSerial :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
572−unfoldrMSerial step seed = fromStreamS (S.unfoldrM step seed)
572+unfoldrMSerial = Serial.unfoldrM
573
573
574-- | Convert an 'Unfold' into a stream by supplying it an input seed.
574-- | Convert an 'Unfold' into a stream by supplying it an input seed.
575--
575--