@@ -57,7 +57,17 @@ module Streamly.Streams
57 , (<=>) --deprecated
57 , (<=>) --deprecated
58 , (<|) --deprecated
58 , (<|) --deprecated
60− -- * Stream Styles
70+ -- * Stream Transformers
62 , CostreamT
72 , CostreamT
63 , InterleavedT -- deprecated
73 , InterleavedT -- deprecated
@@ -111,7 +121,7 @@ import Control.Monad.State.Class (MonadState(..))
111import Control.Monad.Trans.Class (MonadTrans (lift))
121import Control.Monad.Trans.Class (MonadTrans (lift))
112import Data.Semigroup (Semigroup(..))
122import Data.Semigroup (Semigroup(..))
113import Prelude hiding (zipWith)
123import Prelude hiding (zipWith)
114−import Streamly.Core ( MonadParallel, Stream(Stream)
124+import Streamly.Core ( MonadParallel
115 , singleton
125 , singleton
116 , SVar, SVarStyle(..)
126 , SVar, SVarStyle(..)
117 , SVarTag(..), SVarSched(..))
127 , SVarTag(..), SVarSched(..))
@@ -124,8 +134,8 @@ import qualified Streamly.Core as S
124-- | Class of types that can represent a stream of elements of some type 'a' in
134-- | Class of types that can represent a stream of elements of some type 'a' in
125-- some monad 'm'.
135-- some monad 'm'.
126class IsStream t where
136class IsStream t where
127− toStream :: t m a -> Stream m a
137+ toStream :: t m a -> S.Stream m a
128− fromStream :: Stream m a -> t m a
138+ fromStream :: S.Stream m a -> t m a
130-- | Same as 'IsStream'.
140-- | Same as 'IsStream'.
131{-# Deprecated Streaming "Please use IsStream instead." #-}
141{-# Deprecated Streaming "Please use IsStream instead." #-}
@@ -195,13 +205,13 @@ streamBuild :: IsStream t
198−streamBuild k = fromStream $ Stream $ \svr stp sng yld ->
208+streamBuild k = fromStream $ S.Stream $ \svr stp sng yld ->
199 let yield a r = yld a (toStream r)
209 let yield a r = yld a (toStream r)
200 in k svr yield sng stp
210 in k svr yield sng stp
202-- | Build a singleton stream from a callback function.
212-- | Build a singleton stream from a callback function.
203fromCallback :: IsStream t => (forall r. (a -> m r) -> m r) -> t m a
213fromCallback :: IsStream t => (forall r. (a -> m r) -> m r) -> t m a
204−fromCallback k = fromStream $ Stream $ \_ _ sng _ -> k sng
214+fromCallback k = fromStream $ S.Stream $ \_ _ sng _ -> k sng
206-- | Read an SVar to get a stream.
216-- | Read an SVar to get a stream.
207fromSVar :: (MonadParallel m, IsStream t) => SVar m a -> t m a
217fromSVar :: (MonadParallel m, IsStream t) => SVar m a -> t m a
@@ -358,7 +368,7 @@ instance (MonadState s m CONSTRAINT) => MonadState s (STREAM m) where { \
358-- Note that serial composition can be used to combine an infinite number of
368-- Note that serial composition can be used to combine an infinite number of
359-- streams as it explores only one stream at a time.
369-- streams as it explores only one stream at a time.
361−newtype StreamT m a = StreamT {getStreamT :: Stream m a}
371+newtype StreamT m a = StreamT {getStreamT :: S.Stream m a}
362 deriving (Semigroup, Monoid, Functor, MonadTrans)
372 deriving (Semigroup, Monoid, Functor, MonadTrans)
364instance IsStream StreamT where
374instance IsStream StreamT where
@@ -382,7 +392,7 @@ splice m1 m2 = fromStream $ S.splice (toStream m1) (toStream m2)
383instance Monad m => Monad (StreamT m) where
393instance Monad m => Monad (StreamT m) where
384 return = pure
394 return = pure
385− (StreamT (Stream m)) >>= f = StreamT $ Stream $ \_ stp sng yld ->
395+ (StreamT (S.Stream m)) >>= f = StreamT $ S.Stream $ \_ stp sng yld ->
386 let run x = (S.runStream x) Nothing stp sng yld
396 let run x = (S.runStream x) Nothing stp sng yld
387 single a = run $ toStream (f a)
397 single a = run $ toStream (f a)
388 yield a r = run $ toStream $ f a <> (fromStream r >>= f)
398 yield a r = run $ toStream $ f a <> (fromStream r >>= f)
@@ -429,7 +439,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
429-- Note that interleaving composition can only combine a finite number of
439-- Note that interleaving composition can only combine a finite number of
430-- streams as it needs to retain state for each unfinished stream.
440-- streams as it needs to retain state for each unfinished stream.
432−newtype CostreamT m a = CostreamT {getCostreamT :: Stream m a}
442+newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
433 deriving (Functor, MonadTrans)
443 deriving (Functor, MonadTrans)
435{-# DEPRECATED InterleavedT "Please use 'CostreamT' instead." #-}
445{-# DEPRECATED InterleavedT "Please use 'CostreamT' instead." #-}
@@ -474,7 +484,7 @@ instance Monoid (CostreamT m a) where
475instance Monad m => Monad (CostreamT m) where
485instance Monad m => Monad (CostreamT m) where
476 return = pure
486 return = pure
477− (CostreamT (Stream m)) >>= f = CostreamT $ Stream $ \_ stp sng yld ->
487+ (CostreamT (S.Stream m)) >>= f = CostreamT $ S.Stream $ \_ stp sng yld ->
478 let run x = (S.runStream x) Nothing stp sng yld
488 let run x = (S.runStream x) Nothing stp sng yld
479 single a = run $ toStream (f a)
489 single a = run $ toStream (f a)
480 yield a r = run $ toStream $ f a <> (fromStream r >>= f)
490 yield a r = run $ toStream $ f a <> (fromStream r >>= f)
@@ -530,7 +540,7 @@ MONAD_COMMON_INSTANCES(CostreamT,)
530-- Note that this composition can be used to combine infinite number of streams
540-- Note that this composition can be used to combine infinite number of streams
531-- as it explores only a bounded number of streams at a time.
541-- as it explores only a bounded number of streams at a time.
533−newtype CoparallelT m a = CoparallelT {getCoparallelT :: Stream m a}
543+newtype CoparallelT m a = CoparallelT {getCoparallelT :: S.Stream m a}
534 deriving (Functor, MonadTrans)
544 deriving (Functor, MonadTrans)
536{-# DEPRECATED AsyncT "Please use 'CoparallelT' instead." #-}
546{-# DEPRECATED AsyncT "Please use 'CoparallelT' instead." #-}
@@ -574,14 +584,14 @@ instance MonadParallel m => Monoid (CoparallelT m a) where
575{-# INLINE parbind #-}
585{-# INLINE parbind #-}
577− :: (forall c. Stream m c -> Stream m c -> Stream m c)
587+ :: (forall c. S.Stream m c -> S.Stream m c -> S.Stream m c)
578− -> Stream m a
588+ -> S.Stream m a
579− -> (a -> Stream m b)
589+ -> (a -> S.Stream m b)
580− -> Stream m b
590+ -> S.Stream m b
581parbind par m f = go m
591parbind par m f = go m
583− go (Stream g) =
593+ go (S.Stream g) =
584− Stream $ \ctx stp sng yld ->
594+ S.Stream $ \ctx stp sng yld ->
585 let run x = (S.runStream x) ctx stp sng yld
595 let run x = (S.runStream x) ctx stp sng yld
586 single a = run $ f a
596 single a = run $ f a
587 yield a r = run $ f a `par` go r
597 yield a r = run $ f a `par` go r
@@ -640,7 +650,7 @@ MONAD_COMMON_INSTANCES(CoparallelT, MONADPARALLEL)
640-- Note that round robin composition can only combine a finite number of
650-- Note that round robin composition can only combine a finite number of
641-- streams as it needs to retain state for each unfinished stream.
651-- streams as it needs to retain state for each unfinished stream.
643−newtype ParallelT m a = ParallelT {getParallelT :: Stream m a}
653+newtype ParallelT m a = ParallelT {getParallelT :: S.Stream m a}
644 deriving (Functor, MonadTrans)
654 deriving (Functor, MonadTrans)
646instance IsStream ParallelT where
656instance IsStream ParallelT where
@@ -692,7 +702,7 @@ MONAD_COMMON_INSTANCES(ParallelT, MONADPARALLEL)
692zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
702zipWith :: IsStream t => (a -> b -> c) -> t m a -> t m b -> t m c
693zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
703zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
695− go mx my = Stream $ \_ stp sng yld -> do
705+ go mx my = S.Stream $ \_ stp sng yld -> do
696 let merge a ra =
706 let merge a ra =
697 let single2 b = sng (f a b)
707 let single2 b = sng (f a b)
698 yield2 b rb = yld (f a b) (go ra rb)
708 yield2 b rb = yld (f a b) (go ra rb)
@@ -718,7 +728,7 @@ zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
718-- The 'Semigroup' instance of this type works the same way as that of
728-- The 'Semigroup' instance of this type works the same way as that of
719-- 'StreamT'.
729-- 'StreamT'.
721−newtype ZipStreamM m a = ZipStreamM {getZipStreamM :: Stream m a}
731+newtype ZipStreamM m a = ZipStreamM {getZipStreamM :: S.Stream m a}
722 deriving (Functor, Semigroup, Monoid)
732 deriving (Functor, Semigroup, Monoid)
724instance Monad m => Applicative (ZipStreamM m) where
734instance Monad m => Applicative (ZipStreamM m) where
@@ -737,7 +747,7 @@ instance IsStream ZipStreamM where
737-- generated concurrently) using a pure zipping function.
747-- generated concurrently) using a pure zipping function.
738zipParallelWith :: (IsStream t, MonadParallel m)
748zipParallelWith :: (IsStream t, MonadParallel m)
739 => (a -> b -> c) -> t m a -> t m b -> t m c
749 => (a -> b -> c) -> t m a -> t m b -> t m c
740−zipParallelWith f m1 m2 = fromStream $ Stream $ \_ stp sng yld -> do
750+zipParallelWith f m1 m2 = fromStream $ S.Stream $ \_ stp sng yld -> do
741 ma <- async m1
751 ma <- async m1
742 mb <- async m2
752 mb <- async m2
743 (S.runStream (toStream (zipWith f ma mb))) Nothing stp sng yld
753 (S.runStream (toStream (zipWith f ma mb))) Nothing stp sng yld
@@ -763,7 +773,7 @@ zipAsyncWith = zipParallelWith
763-- The 'Semigroup' instance of this type works the same way as that of
773-- The 'Semigroup' instance of this type works the same way as that of
764-- 'StreamT'.
774-- 'StreamT'.
766−newtype ZipParallelM m a = ZipParallelM {getZipParallelM :: Stream m a}
776+newtype ZipParallelM m a = ZipParallelM {getZipParallelM :: S.Stream m a}
767 deriving (Functor, Semigroup, Monoid)
777 deriving (Functor, Semigroup, Monoid)
769{-# DEPRECATED ZipAsync "Please use ZipParallelM instead." #-}
779{-# DEPRECATED ZipAsync "Please use ZipParallelM instead." #-}
@@ -863,6 +873,41 @@ runZipStream = runStream . zipStreamly
863runZipAsync :: Monad m => ZipParallelM m a -> m ()
873runZipAsync :: Monad m => ZipParallelM m a -> m ()
864runZipAsync = runStream . zipParallely
874runZipAsync = runStream . zipParallely
876+------------------------------------------------------------------------------
878+------------------------------------------------------------------------------
880+-- | A serial IO stream of elements of type @a@. See 'StreamT' documentation
882+type Stream a = StreamT IO a
884+-- | An interleaving serial IO stream of elements of type @a@. See 'CostreamT'
885+-- documentation for more details.
886+type Costream a = CostreamT IO a
888+-- | A demand driven left biased parallelly composing IO stream of elements of
889+-- type @a@. See 'CoparallelT' documentation for more details.
890+type Coparallel a = CoparallelT IO a
892+-- | A round robin parallelly composing IO stream of elements of type @a@.
893+-- See 'ParallelT' documentation for more details.
894+type Parallel a = ParallelT IO a
896+-- | An IO stream whose applicative instance zips streams serially.
897+type ZipStream a = ZipStreamM IO a
899+-- | An IO stream whose applicative instance zips streams parallely.
900+type ZipParallel a = ZipParallelM IO a
902+-- | Lift a value from IO monad to any of the stream types. This works for all
903+-- stream types including those that are not monads e.g. 'ZipStream' and
905+fromIO :: (IsStream t, MonadIO m) => IO a -> t m a
906+fromIO = streamly . liftIO
908+fromM :: (IsStream t, Monad m) => m a -> t m a
909+fromM = streamly . lift
866------------------------------------------------------------------------------
911------------------------------------------------------------------------------
867-- Fold Utilities
912-- Fold Utilities
868------------------------------------------------------------------------------
913------------------------------------------------------------------------------