@@ -30,7 +30,7 @@
30-- import qualified Streamly.Streams.StreamD as D
30-- import qualified Streamly.Streams.StreamD as D
33−-- A majority of functions in this file have been adapted from the vector
33+-- Some of functions in this file have been adapted from the vector
34-- library, https://hackage.haskell.org/package/vector.
34-- library, https://hackage.haskell.org/package/vector.
36module Streamly.Streams.StreamD
36module Streamly.Streams.StreamD
@@ -74,6 +74,9 @@ module Streamly.Streams.StreamD
75 -- ** Specialized Folds
75 -- ** Specialized Folds
76 , runStream
76 , runStream
@@ -119,7 +122,7 @@ import GHC.Types ( SPEC(..) )
119import Prelude
122import Prelude
120 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
123 hiding (map, mapM, mapM_, repeat, foldr, last, take, filter,
121 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
124 takeWhile, drop, dropWhile, all, any, maximum, minimum, elem,
122− notElem)
125+ notElem, null, head, tail)
124import Streamly.SVar (MonadAsync)
127import Streamly.SVar (MonadAsync)
125import qualified Streamly.Streams.StreamK as K
128import qualified Streamly.Streams.StreamK as K
@@ -151,7 +154,7 @@ data Stream m a = forall s. Stream (s -> m (Step s a)) s
151nil :: Monad m => Stream m a
154nil :: Monad m => Stream m a
152nil = Stream (const $ return Stop) ()
155nil = Stream (const $ return Stop) ()
154−-- | Note that the cons operation is not scalable as it has O(n^2) complexity.
157+-- | Can fuse but has O(n^2) complexity.
155cons :: Monad m => a -> Stream m a -> Stream m a
158cons :: Monad m => a -> Stream m a -> Stream m a
156cons x (Stream step state) = Stream step1 Nothing
159cons x (Stream step state) = Stream step1 Nothing
@@ -166,6 +169,7 @@ cons x (Stream step state) = Stream step1 Nothing
166-- Deconstruction
169-- Deconstruction
167-------------------------------------------------------------------------------
170-------------------------------------------------------------------------------
172+-- Does not fuse, has the same performance as the StreamK version.
169{-# INLINE_NORMAL uncons #-}
173{-# INLINE_NORMAL uncons #-}
170uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
174uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
171uncons (Stream step state) = go state
175uncons (Stream step state) = go state
@@ -311,6 +315,39 @@ runStream (Stream step state) = go SPEC state
311 Yield _ s -> go SPEC s
315 Yield _ s -> go SPEC s
312 Stop -> return ()
316 Stop -> return ()
318+{-# INLINE_NORMAL null #-}
319+null :: Monad m => Stream m a -> m Bool
320+null (Stream step state) = go state
325+ Yield _ _ -> return False
329+{-# INLINE_NORMAL head #-}
330+head :: Monad m => Stream m a -> m (Maybe a)
331+head (Stream step state) = go state
336+ Yield x _ -> return (Just x)
337+ Stop -> return Nothing
339+-- Does not fuse, has the same performance as the StreamK version.
340+{-# INLINE_NORMAL tail #-}
341+tail :: Monad m => Stream m a -> m (Maybe (Stream m a))
342+tail (Stream step state) = go state
347+ Yield _ s -> return (Just $ Stream step s)
348+ Stop -> return Nothing
350+-- XXX will it fuse? need custom impl?
314{-# INLINE_NORMAL last #-}
351{-# INLINE_NORMAL last #-}
315last :: Monad m => Stream m a -> m (Maybe a)
352last :: Monad m => Stream m a -> m (Maybe a)
316last = foldl' (\_ y -> Just y) Nothing
353last = foldl' (\_ y -> Just y) Nothing