@@ -333,58 +333,13 @@ import Prelude hiding
333333 , (!!), scanl, scanl1, concatMap, replicate, enumFromTo, concat
334334 , reverse, iterate, splitAt)
335335import Streamly.Internal.Data.Stream.StreamD.Type
336+import Streamly.Internal.Data.Stream.StreamD.Common (takeWhileM, takeWhile)
336337import Streamly.Internal.Data.Stream.StreamD.Generate
337338import Streamly.Internal.Data.Stream.StreamD.Eliminate
338339import Streamly.Internal.Data.Stream.StreamD.Flatten
339340import Streamly.Internal.Data.Stream.StreamD.SplitGroup
340341import Streamly.Internal.Data.SVar
341342
342−------------------------------------------------------------------------------
343−-- Construction
344−------------------------------------------------------------------------------
345−
346−-- | An empty 'Stream'.
347−{-# INLINE_NORMAL nil #-}
348−nil :: Monad m => Stream m a
349−nil = Stream (\_ _ -> return Stop) ()
350−
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) ()
355−
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
359− where
360− {-# INLINE_LATE step1 #-}
361− step1 _ Nothing = m >>= \x -> return $ Yield x (Just state)
362− step1 gst (Just st) = do
363− r <- step gst st
364− return $
365− case r of
366− Yield a s -> Yield a (Just s)
367− Skip s -> Skip (Just s)
368− Stop -> Stop
369−
370−-- XXX implement in terms of consM?
371−-- cons x = consM (return x)
372−--
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
377− where
378− {-# INLINE_LATE step1 #-}
379− step1 _ Nothing = return $ Yield x (Just state)
380− step1 gst (Just st) = do
381− r <- step gst st
382− return $
383− case r of
384− Yield a s -> Yield a (Just s)
385− Skip s -> Skip (Just s)
386− Stop -> Stop
387−
388343-------------------------------------------------------------------------------
389344-- Deconstruction
390345-------------------------------------------------------------------------------
@@ -401,38 +356,6 @@ uncons (UnStream step state) = go state
401356 Skip s -> go s
402357 Stop -> return Nothing
403358
404−-- We are assuming that "to" is constrained by the type to be within
405−-- max/min bounds.
406−{-# INLINE enumerateFromToIntegral #-}
407−enumerateFromToIntegral :: (Monad m, Integral a) => a -> a -> Stream m a
408−enumerateFromToIntegral from to =
409− takeWhile (<= to) $ enumerateFromStepIntegral from 1
410−
411−{-# INLINE enumerateFromIntegral #-}
412−enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => a -> Stream m a
413−enumerateFromIntegral from = enumerateFromToIntegral from maxBound
414−
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
424−
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
431− where
432− mid = (next - from) / 2
433− predicate | next >= from = (<= to + mid)
434− | otherwise = (>= to + mid)
435−
436359-------------------------------------------------------------------------------
437360-- Hoisting the inner monad
438361-------------------------------------------------------------------------------
@@ -1332,24 +1255,6 @@ rollingMap f = rollingMapM (\x y -> return $ f x y)
13321255-- Filtering
13331256-------------------------------------------------------------------------------
13341257
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
1338− where
1339− {-# INLINE_LATE step' #-}
1340− step' gst st = do
1341− r <- step gst st
1342− case r of
1343− Yield x s -> do
1344− b <- f x
1345− return $ if b then Yield x s else Stop
1346− Skip s -> return $ Skip s
1347− Stop -> return Stop
1348−
1349−{-# INLINE takeWhile #-}
1350−takeWhile :: Monad m => (a -> Bool) -> Stream m a -> Stream m a
1351−takeWhile f = takeWhileM (return . f)
1352−
13531258{-# INLINE_NORMAL drop #-}
13541259drop :: Monad m => Int -> Stream m a -> Stream m a
13551260drop n (Stream step state) = Stream step' (state, Just n)