@@ -78,14 +78,17 @@ module Streamly.Internal.Data.Stream.StreamK.Type
78 , mapMWith
78 , mapMWith
79 , mapMSerial
79 , mapMSerial
81 , concatMapWith
87 , concatMapWith
82 , concatMap
88 , concatMap
83 , bindWith
89 , bindWith
84 , concatPairsWith
90 , concatPairsWith
@@ -97,20 +100,14 @@ module Streamly.Internal.Data.Stream.StreamK.Type
97 , interleave
100 , interleave
98 , interleaveFst
101 , interleaveFst
99 , interleaveMin
102 , interleaveMin
107−import Control.Monad (ap, (>=>))
106+-- import Control.Applicative (liftA2)
108−import Control.Monad.Trans.Class (MonadTrans(lift))
107+import Control.Monad ((>=>))
109−import Control.Monad.Trans.State.Strict (StateT)
110import Data.Function (fix)
108import Data.Function (fix)
111import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
109import Streamly.Internal.Data.SVar.Type (State, adaptState, defState)
113−import qualified Control.Monad.Trans.State.Strict as State
114import qualified Prelude
111import qualified Prelude
116import Prelude hiding
113import Prelude hiding
@@ -866,14 +863,6 @@ mapMWith cns f = go
866instance Monad m => Functor (Stream m) where
863instance Monad m => Functor (Stream m) where
867 fmap = map
864 fmap = map
869−-------------------------------------------------------------------------------
871−-------------------------------------------------------------------------------
873−instance MonadTrans Stream where
877-------------------------------------------------------------------------------
866-------------------------------------------------------------------------------
878-- Nesting
867-- Nesting
879-------------------------------------------------------------------------------
868-------------------------------------------------------------------------------
@@ -885,13 +874,13 @@ unShare x = mkStream $ \st yld sng stp ->
885 foldStream st yld sng stp x
874 foldStream st yld sng stp x
887-- XXX the function stream and value stream can run in parallel
876-- XXX the function stream and value stream can run in parallel
888−{-# INLINE apWith #-}
877+{-# INLINE crossApplyWith #-}
889−apWith ::
878+crossApplyWith ::
890 (Stream m b -> Stream m b -> Stream m b)
879 (Stream m b -> Stream m b -> Stream m b)
891 -> Stream m (a -> b)
880 -> Stream m (a -> b)
892 -> Stream m a
881 -> Stream m a
893 -> Stream m b
882 -> Stream m b
894−apWith par fstream stream = go1 fstream
883+crossApplyWith par fstream stream = go1 fstream
@@ -908,12 +897,12 @@ apWith par fstream stream = go1 fstream
908 yieldk a r = yld (f a) (go2 f r)
897 yieldk a r = yld (f a) (go2 f r)
909 in foldStream (adaptState st) yieldk single stp m
898 in foldStream (adaptState st) yieldk single stp m
911−{-# INLINE apSerial #-}
900+{-# INLINE crossApply #-}
912−apSerial ::
901+crossApply ::
913 Stream m (a -> b)
902 Stream m (a -> b)
914 -> Stream m a
903 -> Stream m a
915 -> Stream m b
904 -> Stream m b
916−apSerial fstream stream = go1 fstream
905+crossApply fstream stream = go1 fstream
@@ -938,12 +927,12 @@ apSerial fstream stream = go1 fstream
938 yieldk a r = yld (f a) (go3 f r)
927 yieldk a r = yld (f a) (go3 f r)
939 in foldStream (adaptState st) yieldk single stp m
928 in foldStream (adaptState st) yieldk single stp m
941−{-# INLINE apSerialDiscardFst #-}
930+{-# INLINE crossApplySnd #-}
942−apSerialDiscardFst ::
931+crossApplySnd ::
943 Stream m a
932 Stream m a
944 -> Stream m b
933 -> Stream m b
945 -> Stream m b
934 -> Stream m b
946−apSerialDiscardFst fstream stream = go1 fstream
935+crossApplySnd fstream stream = go1 fstream
@@ -962,12 +951,12 @@ apSerialDiscardFst fstream stream = go1 fstream
962 yieldk a r = yld a (go2 r1 r)
951 yieldk a r = yld a (go2 r1 r)
963 in foldStream st yieldk single stop m
952 in foldStream st yieldk single stop m
965−{-# INLINE apSerialDiscardSnd #-}
954+{-# INLINE crossApplyFst #-}
966−apSerialDiscardSnd ::
955+crossApplyFst ::
967 Stream m a
956 Stream m a
968 -> Stream m b
957 -> Stream m b
969 -> Stream m a
958 -> Stream m a
970−apSerialDiscardSnd fstream stream = go1 fstream
959+crossApplyFst fstream stream = go1 fstream
@@ -1116,11 +1105,22 @@ concatPairsWith combine f str = go (leafPairs str)
1116 yieldk a r = yld (a1 `combine` a) $ nonLeafPairs r
1105 yieldk a r = yld (a1 `combine` a) $ nonLeafPairs r
1117 in foldStream (adaptState st) yieldk single stop stream
1106 in foldStream (adaptState st) yieldk single stop stream
1119instance Monad m => Applicative (Stream m) where
1109instance Monad m => Applicative (Stream m) where
1120 {-# INLINE pure #-}
1110 {-# INLINE pure #-}
1121 pure = fromPure
1111 pure = fromPure
1122 {-# INLINE (<*>) #-}
1113 {-# INLINE (<*>) #-}
1123− (<*>) = ap
1114+ (<*>) = crossApply
1116+ {-# INLINE liftA2 #-}
1117+ liftA2 f x = (<*>) (fmap f x)
1119+ {-# INLINE (*>) #-}
1120+ (*>) = crossApplySnd
1122+ {-# INLINE (<*) #-}
1123+ (<*) = crossApplyFst
1125-- NOTE: even though concatMap for StreamD is 3x faster compared to StreamK,
1125-- NOTE: even though concatMap for StreamD is 3x faster compared to StreamK,
1126-- the monad instance of StreamD is slower than StreamK after foldr/build
1126-- the monad instance of StreamD is slower than StreamK after foldr/build
@@ -1128,8 +1128,10 @@ instance Monad m => Applicative (Stream m) where
1128instance Monad m => Monad (Stream m) where
1128instance Monad m => Monad (Stream m) where
1129 {-# INLINE return #-}
1129 {-# INLINE return #-}
1130 return = pure
1130 return = pure
1131 {-# INLINE (>>=) #-}
1132 {-# INLINE (>>=) #-}
1132 (>>=) = flip concatMap
1133 (>>=) = flip concatMap
1135-- Like concatMap but generates stream using an unfold function. Similar to
1137-- Like concatMap but generates stream using an unfold function. Similar to
@@ -1386,34 +1388,3 @@ concatMapEffect :: Monad m => (b -> Stream m a) -> m b -> Stream m a
1386concatMapEffect f action =
1388concatMapEffect f action =
1387 mkStream $ \st yld sng stp ->
1389 mkStream $ \st yld sng stp ->
1388 action >>= foldStreamShared st yld sng stp . f
1390 action >>= foldStreamShared st yld sng stp . f
1390−------------------------------------------------------------------------------
1391−-- Lifting inner monad
1392−------------------------------------------------------------------------------
1394−{-# INLINE evalStateT #-}
1395−evalStateT :: Monad m => m s -> Stream (StateT s m) a -> Stream m a
1400− -- XXX Do not use stream monad
1402− (res, s1) <- lift $ st >>= State.runStateT (uncons m1)
1404− Just (h, t) -> cons h (go (return s1) t)
1407−{-# INLINE liftInner #-}
1408−liftInner :: (Monad m, MonadTrans t, Monad (t m)) =>
1409− Stream m a -> Stream (t m) a
1414− -- XXX Do not use stream monad
1416− res <- fromEffect $ lift $ uncons m1
1418− Just (h, t) -> cons h (go t)