@@ -77,8 +77,8 @@ module Streamly.Internal.Data.Unfold
78 -- * Unfolds
78 -- * Unfolds
79 , fromStream
79 , fromStream
80− , fromStream1
80+ , fromStreamK
81− , fromStream2
81+ , fromStreamD
@@ -317,38 +317,40 @@ mapMWithInput f (Unfold ustep uinject) = Unfold step inject
317-- Convert streams into unfolds
317-- Convert streams into unfolds
318-------------------------------------------------------------------------------
318-------------------------------------------------------------------------------
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
325− Yield x s -> Yield x (Stream step1 s)
326− Skip s -> Skip (Stream step1 s)
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)
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
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 #-}
342+ r <- K.uncons stream
344+ Just (x, xs) -> Yield x xs
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.
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)
353-------------------------------------------------------------------------------
355-------------------------------------------------------------------------------
354-- Unfolds
356-- Unfolds