on master help sign in
Make changes to unfolds from stream
df6d8e0Harendra Kumar
src/Streamly/Internal/Data/Unfold.hs+31−29
@@ -77,8 +77,8 @@ module Streamly.Internal.Data.Unfold
77
77
78 -- * Unfolds
78 -- * Unfolds
79 , fromStream
79 , fromStream
80− , fromStream1
80+ , fromStreamK
81− , fromStream2
81+ , fromStreamD
82 , nilM
82 , nilM
83 , consM
83 , consM
84 , effect
84 , effect
@@ -317,38 +317,40 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
317-- Convert streams into unfolds
317-- Convert streams into unfolds
318-------------------------------------------------------------------------------
318-------------------------------------------------------------------------------
319
319
320−{-# INLINE_LATE streamStep #-}
320+-- this is horribly slow
321+{-# INLINE_NORMAL fromStreamD #-}
321−streamStep :: Monad m => Stream m a -> m (Step (Stream m a) a)
322+fromStreamD :: Monad m => Unfold m (Stream m a) a
322−streamStep (Stream step1 state) = do
323+fromStreamD = Unfold step return
323− r <- step1 defState state
324+ where
324− return $ case r of
325− Yield x s -> Yield x (Stream step1 s)
326− Skip s -> Skip (Stream step1 s)
327− Stop -> Stop
328
325
329−-- | Convert a stream into an 'Unfold'. Note that a stream converted to an
326+ {-# INLINE_LATE step #-}
330−-- 'Unfold' may not be as efficient as an 'Unfold' in some situations.
327+ step (Stream step1 state1) = do
331−--
328+ r <- step1 defState state1
332−-- /Internal/
329+ return $ case r of
333−fromStream :: (K.IsStream t, Monad m) => t m b -> Unfold m Void b
330+ Yield x s -> Yield x (Stream step1 s)
334−fromStream str = Unfold streamStep (\_ -> return $ D.toStreamD str)
331+ Skip s -> Skip (Stream step1 s)
332+ Stop -> Stop
335
333
336−-- | Convert a single argument stream generator function into an
334+{-# INLINE_NORMAL fromStreamK #-}
337−-- 'Unfold'. Note that a stream converted to an 'Unfold' may not be as
335+fromStreamK :: Monad m => Unfold m (K.Stream m a) a
338−-- efficient as an 'Unfold' in some situations.
336+fromStreamK = Unfold step return
339−--
337+
340−-- /Internal/
338+ where
341−fromStream1 :: (K.IsStream t, Monad m) => (a -> t m b) -> Unfold m a b
339+
342−fromStream1 f = Unfold streamStep (return . D.toStreamD . f)
340+ {-# INLINE_LATE step #-}
341+ step stream = do
342+ r <- K.uncons stream
343+ return $ case r of
344+ Just (x, xs) -> Yield x xs
345+ Nothing -> Stop
343
346
344−-- | Convert a two argument stream generator function into an 'Unfold'. Note
347+-- | Convert a stream into an 'Unfold'. Note that a stream converted to an
345−-- that a stream converted to an 'Unfold' may not be as efficient as an
346−-- 'Unfold' in some situations.
348+-- 'Unfold' may not be as efficient as an 'Unfold' in some situations.
347--
349--
348-- /Internal/
350-- /Internal/
351+{-# INLINE_NORMAL fromStream #-}
349−fromStream2 :: (K.IsStream t, Monad m)
352+fromStream :: (K.IsStream t, Monad m) => Unfold m (t m a) a
350− => (a -> b -> t m c) -> Unfold m (a, b) c
353+fromStream = lmap K.toStream fromStreamK
351−fromStream2 f = Unfold streamStep (\(a, b) -> return $ D.toStreamD $ f a b)
352
354
353-------------------------------------------------------------------------------
355-------------------------------------------------------------------------------
354-- Unfolds
356-- Unfolds
src/Streamly/Internal/Prelude.hs+1
@@ -3038,6 +3038,7 @@ concatMapWith = K.concatMapBy
3038-- @
3038-- @
3039-- concatMap = 'concatMapWith' 'Serial.serial'
3039-- concatMap = 'concatMapWith' 'Serial.serial'
3040-- concatMap f = 'concatMapM' (return . f)
3040-- concatMap f = 'concatMapM' (return . f)
3041+-- concatMap f = 'concatUnfold' (UF.lmap f UF.fromStream)
3041-- @
3042-- @
3042--
3043--
3043-- @since 0.6.0
3044-- @since 0.6.0