@@ -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,59 @@ 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 single element (singleton) stream from a monadic action.
160+-- > toList $ once getLine
164+once :: (IsStream t, Monad m) => m a -> t m a
165+once = fromStream . S.once
169+-- | Constructs a stream by adding a monadic action at the head of an existing
170+-- stream. For example:
173+-- > toList $ getLine \`consM` getLine \`consM` nil
178+consM :: (IsStream t, Monad m) => m a -> t m a -> t m a
179+consM m r = fromStream $ S.consM m (toStream r)
183+-- | Operator equivalent of 'consM'.
186+-- > toList $ getLine |: getLine |: nil
191+(|:) :: (IsStream t, Monad m) => m a -> t m a -> t m a
152infixr 5 `cons`
194infixr 5 `cons`
154−-- | Constructs a stream by adding a pure value at the head of an existing
196+-- | Same as @consM . return@. Constructs a stream by adding a pure value at
155−-- stream, just like ':' constructs lists. For example:
197+-- the head of an existing stream. For example:
158−-- > let stream = 1 \`cons` 2 \`cons` 3 \`cons` nil
200+-- > toList $ 1 \`cons` 2 \`cons` 3 \`cons` nil
159−-- > (toList . streamly) stream
160-- [1,2,3]
201-- [1,2,3]
162cons :: IsStream t => a -> t m a -> t m a
203cons :: IsStream t => a -> t m a -> t m a
@@ -164,32 +205,13 @@ cons a r = fromStream $ S.cons a (toStream r)
165infixr 5 .:
206infixr 5 .:
167−-- | Operator equivalent of 'cons' so that you can construct a stream of pure
208+-- | Operator equivalent of 'cons'.
168−-- values more succinctly like this:
171−-- > let stream = 1 .: 2 .: 3 .: nil
211+-- > toList $ 1 .: 2 .: 3 .: nil
172−-- > (toList . streamly) stream
173-- [1,2,3]
212-- [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
215(.:) :: IsStream t => a -> t m a -> t m a
194(.:) = cons
216(.:) = cons
@@ -285,7 +307,7 @@ async m = do
286#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
308#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
287instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
309instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
288− pure = STREAM . singleton; \
310+ pure = STREAM . S.singleton; \
289 (<*>) = ap }
311 (<*>) = ap }
291#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
313#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
@@ -413,7 +435,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
413-- yielding one element from each stream alternately.
435-- yielding one element from each stream alternately.
416−-- main = ('toList' . 'interleaving' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
438+-- main = ('toList' . 'costreamly' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
419-- [1,3,2,4]
441-- [1,3,2,4]
@@ -424,7 +446,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
427−-- main = 'runStream' . 'interleaving' $ do
449+-- main = 'runStream' . 'costreamly' $ do
428-- x <- return 1 \<\> return 2
450-- x <- return 1 \<\> return 2
429-- y <- return 3 \<\> return 4
451-- y <- return 3 \<\> return 4
430-- liftIO $ print (x, y)
452-- liftIO $ print (x, y)
@@ -436,7 +458,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
439−-- Note that interleaving composition can only combine a finite number of
461+-- Note that costreamly composition can only combine a finite number of
440-- streams as it needs to retain state for each unfinished stream.
462-- streams as it needs to retain state for each unfinished stream.
442newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
464newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
@@ -899,15 +921,6 @@ type ZipStream a = ZipStreamM IO a
899-- | An IO stream whose applicative instance zips streams parallely.
921-- | An IO stream whose applicative instance zips streams parallely.
900type ZipParallel a = ZipParallelM IO a
922type 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------------------------------------------------------------------------------
924------------------------------------------------------------------------------
912-- Fold Utilities
925-- Fold Utilities
913------------------------------------------------------------------------------
926------------------------------------------------------------------------------