@@ -333,58 +333,13 @@ import Prelude hiding
333 , (!!), scanl, scanl1, concatMap, replicate, enumFromTo, concat
333 , (!!), scanl, scanl1, concatMap, replicate, enumFromTo, concat
334 , reverse, iterate, splitAt)
334 , reverse, iterate, splitAt)
335import Streamly.Internal.Data.Stream.StreamD.Type
335import Streamly.Internal.Data.Stream.StreamD.Type
336+import Streamly.Internal.Data.Stream.StreamD.Common (takeWhileM, takeWhile)
336import Streamly.Internal.Data.Stream.StreamD.Generate
337import Streamly.Internal.Data.Stream.StreamD.Generate
337import Streamly.Internal.Data.Stream.StreamD.Eliminate
338import Streamly.Internal.Data.Stream.StreamD.Eliminate
338import Streamly.Internal.Data.Stream.StreamD.Flatten
339import Streamly.Internal.Data.Stream.StreamD.Flatten
339import Streamly.Internal.Data.Stream.StreamD.SplitGroup
340import Streamly.Internal.Data.Stream.StreamD.SplitGroup
340import Streamly.Internal.Data.SVar
341import Streamly.Internal.Data.SVar
342−------------------------------------------------------------------------------
344−------------------------------------------------------------------------------
346−-- | An empty 'Stream'.
347−{-# INLINE_NORMAL nil #-}
348−nil :: Monad m => Stream m a
349−nil = Stream (\_ _ -> return Stop) ()
351−-- | An empty 'Stream' with a side effect.
352−{-# INLINE_NORMAL nilM #-}
353−nilM :: Monad m => m b -> Stream m a
354−nilM m = Stream (\_ _ -> m >> return Stop) ()
356−{-# INLINE_NORMAL consM #-}
357−consM :: Monad m => m a -> Stream m a -> Stream m a
358−consM m (Stream step state) = Stream step1 Nothing
360− {-# INLINE_LATE step1 #-}
361− step1 _ Nothing = m >>= \x -> return $ Yield x (Just state)
362− step1 gst (Just st) = do
366− Yield a s -> Yield a (Just s)
367− Skip s -> Skip (Just s)
370−-- XXX implement in terms of consM?
371−-- cons x = consM (return x)
373−-- | Can fuse but has O(n^2) complexity.
374−{-# INLINE_NORMAL cons #-}
375−cons :: Monad m => a -> Stream m a -> Stream m a
376−cons x (Stream step state) = Stream step1 Nothing
378− {-# INLINE_LATE step1 #-}
379− step1 _ Nothing = return $ Yield x (Just state)
380− step1 gst (Just st) = do
384− Yield a s -> Yield a (Just s)
385− Skip s -> Skip (Just s)
388-------------------------------------------------------------------------------
343-------------------------------------------------------------------------------
389-- Deconstruction
344-- Deconstruction
390-------------------------------------------------------------------------------
345-------------------------------------------------------------------------------
@@ -401,38 +356,6 @@ uncons (UnStream step state) = go state
401 Skip s -> go s
356 Skip s -> go s
402 Stop -> return Nothing
357 Stop -> return Nothing
404−-- We are assuming that "to" is constrained by the type to be within
406−{-# INLINE enumerateFromToIntegral #-}
407−enumerateFromToIntegral :: (Monad m, Integral a) => a -> a -> Stream m a
408−enumerateFromToIntegral from to =
409− takeWhile (<= to) $ enumerateFromStepIntegral from 1
411−{-# INLINE enumerateFromIntegral #-}
412−enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => a -> Stream m a
413−enumerateFromIntegral from = enumerateFromToIntegral from maxBound
415−-- We cannot write a general function for Num. The only way to write code
416−-- portable between the two is to use a 'Real' constraint and convert between
417−-- Fractional and Integral using fromRational which is horribly slow.
418−{-# INLINE_NORMAL enumerateFromToFractional #-}
419−enumerateFromToFractional
420− :: (Monad m, Fractional a, Ord a)
421− => a -> a -> Stream m a
422−enumerateFromToFractional from to =
423− takeWhile (<= to + 1 / 2) $ enumerateFromStepNum from 1
425−{-# INLINE_NORMAL enumerateFromThenToFractional #-}
426−enumerateFromThenToFractional
427− :: (Monad m, Fractional a, Ord a)
428− => a -> a -> a -> Stream m a
429−enumerateFromThenToFractional from next to =
430− takeWhile predicate $ numFromThen from next
432− mid = (next - from) / 2
433− predicate | next >= from = (<= to + mid)
434− | otherwise = (>= to + mid)
436-------------------------------------------------------------------------------
359-------------------------------------------------------------------------------
437-- Hoisting the inner monad
360-- Hoisting the inner monad
438-------------------------------------------------------------------------------
361-------------------------------------------------------------------------------
@@ -1332,24 +1255,6 @@ rollingMap f = rollingMapM (\x y -> return $ f x y)
1332-- Filtering
1255-- Filtering
1333-------------------------------------------------------------------------------
1256-------------------------------------------------------------------------------
1335−{-# INLINE_NORMAL takeWhileM #-}
1336−takeWhileM :: Monad m => (a -> m Bool) -> Stream m a -> Stream m a
1337−takeWhileM f (Stream step state) = Stream step' state
1339− {-# INLINE_LATE step' #-}
1345− return $ if b then Yield x s else Stop
1346− Skip s -> return $ Skip s
1347− Stop -> return Stop
1349−{-# INLINE takeWhile #-}
1350−takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
1351−takeWhile f = takeWhileM (return . f)
1353{-# INLINE_NORMAL drop #-}
1258{-# INLINE_NORMAL drop #-}
1354drop :: Monad m => Int -> Stream m a -> Stream m a
1259drop :: Monad m => Int -> Stream m a -> Stream m a
1355drop n (Stream step state) = Stream step' (state, Just n)
1260drop n (Stream step state) = Stream step' (state, Just n)