on master help sign in
Use bindWith to implement all the binds
c175cddHarendra Kumar
src/Streamly/Streams/Ahead.hs+2−19
@@ -49,7 +49,7 @@ import Streamly.Streams.Serial (map)
49import Streamly.SVar
49import Streamly.SVar
50import Streamly.Streams.StreamK
50import Streamly.Streams.StreamK
51 (IsStream(..), Stream, mkStream, foldStream, foldStreamShared,
51 (IsStream(..), Stream, mkStream, foldStream, foldStreamShared,
52− foldStreamSVar, unShare)
52+ foldStreamSVar)
53import qualified Streamly.Streams.StreamK as K
53import qualified Streamly.Streams.StreamK as K
54
54
55import Prelude hiding (map)
55import Prelude hiding (map)
@@ -677,26 +677,9 @@ instance MonadAsync m => Monoid (AheadT m a) where
677-- Monad
677-- Monad
678------------------------------------------------------------------------------
678------------------------------------------------------------------------------
679
679
680−-- XXX use bindWith instead
681−
682−{-# INLINE aheadbind #-}
683−aheadbind
684− :: MonadAsync m
685− => Stream m a
686− -> (a -> Stream m b)
687− -> Stream m b
688−aheadbind m f = go m
689− where
690− go g =
691− mkStream $ \st yld sng stp ->
692− let foldShared = foldStreamShared st yld sng stp
693− single a = foldShared $ unShare (f a)
694− yieldk a r = foldShared $ unShare (f a) `ahead` go r
695− in foldStream (adaptState st) yieldk single stp g
696−
697instance MonadAsync m => Monad (AheadT m) where
680instance MonadAsync m => Monad (AheadT m) where
698 return = pure
681 return = pure
699− (AheadT m) >>= f = AheadT $ aheadbind m (getAheadT . f)
682+ (>>=) = K.bindWith ahead
700
683
701------------------------------------------------------------------------------
684------------------------------------------------------------------------------
702-- Other instances
685-- Other instances
src/Streamly/Streams/Serial.hs+2−19
@@ -60,7 +60,6 @@ import Text.Read (Lexeme(Ident), lexP, parens, prec, readPrec, readListPrec,
60 readListPrecDefault)
60 readListPrecDefault)
61import Prelude hiding (map, mapM)
61import Prelude hiding (map, mapM)
62
62
63−import Streamly.SVar (adaptState)
64import Streamly.Streams.StreamK (IsStream(..), adapt, Stream, mkStream,
63import Streamly.Streams.StreamK (IsStream(..), adapt, Stream, mkStream,
65 foldStream)
64 foldStream)
66import qualified Streamly.Streams.Prelude as P
65import qualified Streamly.Streams.Prelude as P
@@ -168,17 +167,9 @@ instance IsStream SerialT where
168-- Monad
167-- Monad
169------------------------------------------------------------------------------
168------------------------------------------------------------------------------
170
169
171−-- XXX if foldStreamShared/unShare/adaptState are not efficient in serial case
172−-- we can use a rewrite rule for that for the serial case and then just use a
173−-- bindWith serial here.
174−--
175instance Monad m => Monad (SerialT m) where
170instance Monad m => Monad (SerialT m) where
176 return = pure
171 return = pure
177− m >>= f = mkStream $ \st yld sng stp ->
172+ (>>=) = K.bindWith K.serial
178− let run x = foldStream st yld sng stp x
179− single a = run $ f a
180− yieldk a r = run $ f a <> (r >>= f)
181− in foldStream (adaptState st) yieldk single stp m
182
173
183------------------------------------------------------------------------------
174------------------------------------------------------------------------------
184-- Other instances
175-- Other instances
@@ -331,17 +322,9 @@ instance Monoid (WSerialT m a) where
331-- Monad
322-- Monad
332------------------------------------------------------------------------------
323------------------------------------------------------------------------------
333
324
334−-- XXX if foldStreamShared/unShare/adaptState are not efficient in serial case
335−-- we can use a rewrite rule for that for the serial case and then just use a
336−-- bindWith interleave here.
337−--
338instance Monad m => Monad (WSerialT m) where
325instance Monad m => Monad (WSerialT m) where
339 return = pure
326 return = pure
340− m >>= f = mkStream $ \st yld sng stp ->
327+ (>>=) = K.bindWith wSerial
341− let run x = foldStream st yld sng stp x
342− single a = run $ f a
343− yieldk a r = run $ f a <> (r >>= f)
344− in foldStream (adaptState st) yieldk single stp m
345
328
346------------------------------------------------------------------------------
329------------------------------------------------------------------------------
347-- Other instances
330-- Other instances