on master help sign in
Move uncons,foldr1 to StreamD.Common,StreamD.Eliminate.
c996014Pranay Sashank
src/Streamly/Internal/Data/Stream/StreamD.hs−32
@@ -335,22 +335,6 @@ import Streamly.Internal.Data.Stream.StreamD.Flatten
335import Streamly.Internal.Data.Stream.StreamD.SplitGroup
335import Streamly.Internal.Data.Stream.StreamD.SplitGroup
336import Streamly.Internal.Data.SVar
336import Streamly.Internal.Data.SVar
337
337
338−-------------------------------------------------------------------------------
339−-- Deconstruction
340−-------------------------------------------------------------------------------
341−
342−-- Does not fuse, has the same performance as the StreamK version.
343−{-# INLINE_NORMAL uncons #-}
344−uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
345−uncons (UnStream step state) = go state
346− where
347− go st = do
348− r <- step defState st
349− case r of
350− Yield x s -> return $ Just (x, Stream step s)
351− Skip s -> go s
352− Stop -> return Nothing
353−
354-------------------------------------------------------------------------------
338-------------------------------------------------------------------------------
355-- Hoisting the inner monad
339-- Hoisting the inner monad
356-------------------------------------------------------------------------------
340-------------------------------------------------------------------------------
@@ -423,22 +407,6 @@ runStateT initial (Stream step state) = Stream step' (state, initial)
423 Skip s -> Skip (s, return sv')
407 Skip s -> Skip (s, return sv')
424 Stop -> Stop
408 Stop -> Stop
425
409
426−------------------------------------------------------------------------------
427−-- Elimination by Folds
428−------------------------------------------------------------------------------
429−
430−------------------------------------------------------------------------------
431−-- Right Folds
432−------------------------------------------------------------------------------
433−
434−{-# INLINE_NORMAL foldr1 #-}
435−foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
436−foldr1 f m = do
437− r <- uncons m
438− case r of
439− Nothing -> return Nothing
440− Just (h, t) -> fmap Just (foldr f h t)
441−
442------------------------------------------------------------------------------
410------------------------------------------------------------------------------
443-- Substreams
411-- Substreams
444------------------------------------------------------------------------------
412------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/StreamD/Common.hs+21
@@ -15,6 +15,7 @@ module Streamly.Internal.Data.Stream.StreamD.Common
15 FromSVarState (..)
15 FromSVarState (..)
16 , fromProducer
16 , fromProducer
17 , fromPrimIORef
17 , fromPrimIORef
18+ , uncons
18 , nilM
19 , nilM
19 , takeWhileM
20 , takeWhileM
20 , takeWhile
21 , takeWhile
@@ -39,6 +40,26 @@ import Prelude hiding
39import Streamly.Internal.Data.Stream.StreamD.Type
40import Streamly.Internal.Data.Stream.StreamD.Type
40import Streamly.Internal.Data.SVar
41import Streamly.Internal.Data.SVar
41
42
43+-------------------------------------------------------------------------------
44+-- Deconstruction
45+-------------------------------------------------------------------------------
46+
47+-- Does not fuse, has the same performance as the StreamK version.
48+{-# INLINE_NORMAL uncons #-}
49+uncons :: Monad m => Stream m a -> m (Maybe (a, Stream m a))
50+uncons (UnStream step state) = go state
51+ where
52+ go st = do
53+ r <- step defState st
54+ case r of
55+ Yield x s -> return $ Just (x, Stream step s)
56+ Skip s -> go s
57+ Stop -> return Nothing
58+
59+-------------------------------------------------------------------------------
60+-- Construction
61+-------------------------------------------------------------------------------
62+
42-- | An empty 'Stream' with a side effect.
63-- | An empty 'Stream' with a side effect.
43{-# INLINE_NORMAL nilM #-}
64{-# INLINE_NORMAL nilM #-}
44nilM :: Monad m => m b -> Stream m a
65nilM :: Monad m => m b -> Stream m a
src/Streamly/Internal/Data/Stream/StreamD/Eliminate.hs+14
@@ -19,6 +19,7 @@ module Streamly.Internal.Data.Stream.StreamD.Eliminate
19 , foldrM
19 , foldrM
20 , foldrMx
20 , foldrMx
21 , foldr
21 , foldr
22+ , foldr1
22
23
23 , foldl'
24 , foldl'
24 , foldlM'
25 , foldlM'
@@ -103,10 +104,23 @@ import Streamly.Internal.Data.Stream.StreamD.Type
103import Streamly.Internal.Data.Stream.StreamD.Common
104import Streamly.Internal.Data.Stream.StreamD.Common
104import Streamly.Internal.Data.SVar
105import Streamly.Internal.Data.SVar
105
106
107+
106------------------------------------------------------------------------------
108------------------------------------------------------------------------------
107-- Elimination by Folds
109-- Elimination by Folds
108------------------------------------------------------------------------------
110------------------------------------------------------------------------------
109
111
112+------------------------------------------------------------------------------
113+-- Right Folds
114+------------------------------------------------------------------------------
115+
116+{-# INLINE_NORMAL foldr1 #-}
117+foldr1 :: Monad m => (a -> a -> a) -> Stream m a -> m (Maybe a)
118+foldr1 f m = do
119+ r <- uncons m
120+ case r of
121+ Nothing -> return Nothing
122+ Just (h, t) -> fmap Just (foldr f h t)
123+
110------------------------------------------------------------------------------
124------------------------------------------------------------------------------
111-- Left Folds
125-- Left Folds
112------------------------------------------------------------------------------
126------------------------------------------------------------------------------