@@ -34,8 +34,11 @@ module Streamly.Streams
35 -- * Construction
35 -- * Construction
39 , streamBuild
42 , streamBuild
40 , fromCallback
43 , fromCallback
41 , fromSVar
44 , fromSVar
@@ -64,8 +67,6 @@ module Streamly.Streams
64 , Parallel
67 , Parallel
65 , ZipStream
68 , ZipStream
66 , ZipParallel
69 , ZipParallel
70 -- * Stream Transformers
71 -- * Stream Transformers
@@ -122,7 +123,6 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
122import Data.Semigroup (Semigroup(..))
123import Data.Semigroup (Semigroup(..))
123import Prelude hiding (zipWith)
124import Prelude hiding (zipWith)
124import Streamly.Core ( MonadParallel
125import Streamly.Core ( MonadParallel
126 , SVar, SVarStyle(..)
126 , SVar, SVarStyle(..)
127 , SVarTag(..), SVarSched(..))
127 , SVarTag(..), SVarSched(..))
128import qualified Streamly.Core as S
128import qualified Streamly.Core as S
@@ -145,18 +145,60 @@ type Streaming = IsStream
145-- Constructing a stream
145-- Constructing a stream
146------------------------------------------------------------------------------
146------------------------------------------------------------------------------
148−-- | Represesnts an empty stream just like @[]@ represents an empty list.
148+-- | An empty stream.
149nil :: IsStream t => t m a
154nil :: IsStream t => t m a
150nil = fromStream S.nil
155nil = fromStream S.nil
157+-- | Create a singleton stream from a monadic action. Same as @m \`consM` nil@
158+-- but more efficient.
161+-- > toList $ once getLine
165+once :: (IsStream t, Monad m) => m a -> t m a
166+once = fromStream . S.once
170+-- | Constructs a stream by adding a monadic action at the head of an existing
171+-- stream. For example:
174+-- > toList $ getLine \`consM` getLine \`consM` nil
179+consM :: (IsStream t, Monad m) => m a -> t m a -> t m a
180+consM m r = fromStream $ S.consM m (toStream r)
184+-- | Operator equivalent of 'consM'.
187+-- > toList $ getLine |: getLine |: nil
192+(|:) :: (IsStream t, Monad m) => m a -> t m a -> t m a
152infixr 5 `cons`
195infixr 5 `cons`
154−-- | Constructs a stream by adding a pure value at the head of an existing
197+-- | Construct a stream by adding a pure value at the head of an existing
155−-- stream, just like ':' constructs lists. For example:
198+-- stream. Same as @consM . return@. For example:
158−-- > let stream = 1 \`cons` 2 \`cons` 3 \`cons` nil
201+-- > toList $ 1 \`cons` 2 \`cons` 3 \`cons` nil
159−-- > (toList . streamly) stream
160-- [1,2,3]
202-- [1,2,3]
162cons :: IsStream t => a -> t m a -> t m a
204cons :: IsStream t => a -> t m a -> t m a
@@ -164,32 +206,13 @@ cons a r = fromStream $ S.cons a (toStream r)
165infixr 5 .:
207infixr 5 .:
167−-- | Operator equivalent of 'cons' so that you can construct a stream of pure
209+-- | Operator equivalent of 'cons'.
168−-- values more succinctly like this:
171−-- > let stream = 1 .: 2 .: 3 .: nil
212+-- > toList $ 1 .: 2 .: 3 .: nil
172−-- > (toList . streamly) stream
173-- [1,2,3]
213-- [1,2,3]
176−-- '.:' constructs a stream just like ':' constructs a list.
178−-- Also note that another equivalent way of building streams from pure values
182−-- > let stream = pure 1 <> pure 2 <> pure 3
183−-- > (toList . streamly) stream
187−-- In the first method we construct a stream by adding one element at a time.
188−-- In the second method we first construct singleton streams using 'pure' and
189−-- then compose all those streams together using the 'Semigroup' style
190−-- composition of streams. The former method is a bit more efficient than the
193(.:) :: IsStream t => a -> t m a -> t m a
216(.:) :: IsStream t => a -> t m a -> t m a
194(.:) = cons
217(.:) = cons
@@ -285,7 +308,7 @@ async m = do
286#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
309#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
287instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
310instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
288− pure = STREAM . singleton; \
311+ pure = STREAM . S.singleton; \
289 (<*>) = ap }
312 (<*>) = ap }
291#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
314#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
@@ -413,7 +436,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
413-- yielding one element from each stream alternately.
436-- yielding one element from each stream alternately.
416−-- main = ('toList' . 'interleaving' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
439+-- main = ('toList' . 'costreamly' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
419-- [1,3,2,4]
442-- [1,3,2,4]
@@ -424,7 +447,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
427−-- main = 'runStream' . 'interleaving' $ do
450+-- main = 'runStream' . 'costreamly' $ do
428-- x <- return 1 \<\> return 2
451-- x <- return 1 \<\> return 2
429-- y <- return 3 \<\> return 4
452-- y <- return 3 \<\> return 4
430-- liftIO $ print (x, y)
453-- liftIO $ print (x, y)
@@ -436,7 +459,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
439−-- Note that interleaving composition can only combine a finite number of
462+-- Note that costreamly composition can only combine a finite number of
440-- streams as it needs to retain state for each unfinished stream.
463-- streams as it needs to retain state for each unfinished stream.
442newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
465newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
@@ -899,15 +922,6 @@ type ZipStream a = ZipStreamM IO a
899-- | An IO stream whose applicative instance zips streams parallely.
922-- | An IO stream whose applicative instance zips streams parallely.
900type ZipParallel a = ZipParallelM IO a
923type 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
911------------------------------------------------------------------------------
925------------------------------------------------------------------------------
912-- Fold Utilities
926-- Fold Utilities
913------------------------------------------------------------------------------
927------------------------------------------------------------------------------