@@ -1,4 +1,5 @@
1{-# LANGUAGE UndecidableInstances #-}
1{-# LANGUAGE UndecidableInstances #-}
2+{-# OPTIONS_GHC -Wno-orphans #-}
4-- Module : Streamly.Internal.Data.Stream.Serial
5-- Module : Streamly.Internal.Data.Stream.Serial
@@ -65,13 +66,15 @@ import Streamly.Internal.BaseCompat ((#.))
65import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
66import Streamly.Internal.Data.Maybe.Strict (Maybe'(..), toMaybe)
66import Streamly.Data.Stream (Stream)
67import Streamly.Data.Stream (Stream)
69+import qualified Streamly.Data.Fold as Fold
68import qualified Streamly.Data.Stream as Stream
70import qualified Streamly.Data.Stream as Stream
71+import qualified Streamly.Internal.Data.Stream as Stream (toStreamK, fromStreamK)
69import qualified Streamly.Internal.Data.Stream.Common as P
72import qualified Streamly.Internal.Data.Stream.Common as P
70import qualified Streamly.Internal.Data.Stream.StreamD as D
73import qualified Streamly.Internal.Data.Stream.StreamD as D
71 (fromStreamK, toStreamK, mapM)
74 (fromStreamK, toStreamK, mapM)
72import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
75import qualified Streamly.Internal.Data.Stream.StreamK.Type as K
73− (Stream, cons, consM, nil, concatMapWith, fromPure, bindWith
76+ (Stream, mkStream, foldStream, cons, consM, nil, concatMapWith, fromPure
74− , withLocal, interleave, interleaveFst, interleaveMin)
77+ , bindWith, interleave, interleaveFst, interleaveMin)
76import Prelude hiding (map, mapM, repeat, filter)
79import Prelude hiding (map, mapM, repeat, filter)
@@ -83,6 +86,45 @@ import Prelude hiding (map, mapM, repeat, filter)
83-- >>> import qualified Streamly.Data.Stream as Stream
86-- >>> import qualified Streamly.Data.Stream as Stream
84-- >>> import qualified Streamly.Prelude as IsStream
87-- >>> import qualified Streamly.Prelude as IsStream
89+{-# INLINABLE withLocal #-}
90+withLocal :: MonadReader r m => (r -> r) -> K.Stream m a -> K.Stream m a
92+ K.mkStream $ \st yld sng stp ->
93+ let single = local f . sng
94+ yieldk a r = local f $ yld a (withLocal f r)
95+ in K.foldStream st yieldk single (local f stp) m
97+------------------------------------------------------------------------------
98+-- mtl orphan instances
99+------------------------------------------------------------------------------
101+instance (MonadReader r m) => MonadReader r (Stream m) where
104+ local f m = Stream.fromStreamK $ withLocal f (Stream.toStreamK m)
106+instance (MonadState s m) => MonadState s (Stream m) where
111+ put x = lift (put x)
113+ {-# INLINE state #-}
114+ state k = lift (state k)
116+------------------------------------------------------------------------------
117+-- NFData - orphan instances
118+------------------------------------------------------------------------------
120+instance NFData a => NFData (Stream Identity a) where
122+ rnf xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> rnf x) ()) xs
124+instance NFData1 (Stream Identity) where
125+ {-# INLINE liftRnf #-}
126+ liftRnf f xs = runIdentity $ Stream.fold (Fold.foldl' (\_ x -> f x) ()) xs
86------------------------------------------------------------------------------
128------------------------------------------------------------------------------
87-- SerialT
129-- SerialT
88------------------------------------------------------------------------------
130------------------------------------------------------------------------------