on master help sign in
Move uncons,foldr1 to StreamD.Common,StreamD.Eliminate.
commitc9960146f8db497b3dbbac1118428042d0f4307c authorPranay Sashank <pranaysashank@composewell.com> dateMon 21 Dec 2020, 14:20 (6 years ago) parentd0d4850
3 files changed+35−32
src/Streamly/Internal/Data/Stream/StreamD.hs−32 src/Streamly/Internal/Data/Stream/StreamD/Common.hs+21 src/Streamly/Internal/Data/Stream/StreamD/Eliminate.hs+14
src/Streamly/Internal/Data/Stream/StreamD.hs−32
@@ -335,22 +335,6 @@ import Streamly.Internal.Data.Stream.StreamD.Flatten
335335import Streamly.Internal.Data.Stream.StreamD.SplitGroup
336336import Streamly.Internal.Data.SVar
337337
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−
354338-------------------------------------------------------------------------------
355339-- Hoisting the inner monad
356340-------------------------------------------------------------------------------
@@ -423,22 +407,6 @@ runStateT initial (Stream step state) = Stream step' (state, initial)
423407 Skip s -> Skip (s, return sv')
424408 Stop -> Stop
425409
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−
442410------------------------------------------------------------------------------
443411-- Substreams
444412------------------------------------------------------------------------------
src/Streamly/Internal/Data/Stream/StreamD/Common.hs+21
@@ -15,6 +15,7 @@ module Streamly.Internal.Data.Stream.StreamD.Common
1515 FromSVarState (..)
1616 , fromProducer
1717 , fromPrimIORef
18+ , uncons
1819 , nilM
1920 , takeWhileM
2021 , takeWhile
@@ -39,6 +40,26 @@ import Prelude hiding
3940import Streamly.Internal.Data.Stream.StreamD.Type
4041import Streamly.Internal.Data.SVar
4142
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+
4263-- | An empty 'Stream' with a side effect.
4364{-# INLINE_NORMAL nilM #-}
4465nilM :: 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
1919 , foldrM
2020 , foldrMx
2121 , foldr
22+ , foldr1
2223
2324 , foldl'
2425 , foldlM'
@@ -103,10 +104,23 @@ import Streamly.Internal.Data.Stream.StreamD.Type
103104import Streamly.Internal.Data.Stream.StreamD.Common
104105import Streamly.Internal.Data.SVar
105106
107+
106108------------------------------------------------------------------------------
107109-- Elimination by Folds
108110------------------------------------------------------------------------------
109111
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+
110124------------------------------------------------------------------------------
111125-- Left Folds
112126------------------------------------------------------------------------------