@@ -34,8 +34,11 @@ module Streamly.Streams
3434
3535 -- * Construction
3636 , nil
37+ , once
3738 , cons
3839 , (.:)
40+ , consM
41+ , (|:)
3942 , streamBuild
4043 , fromCallback
4144 , fromSVar
@@ -64,8 +67,6 @@ module Streamly.Streams
6467 , Parallel
6568 , ZipStream
6669 , ZipParallel
67− , fromIO
68− , fromM
6970
7071 -- * Stream Transformers
7172 , StreamT
@@ -122,7 +123,6 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
122123import Data.Semigroup (Semigroup(..))
123124import Prelude hiding (zipWith)
124125import Streamly.Core ( MonadParallel
125− , singleton
126126 , SVar, SVarStyle(..)
127127 , SVarTag(..), SVarSched(..))
128128import qualified Streamly.Core as S
@@ -145,18 +145,60 @@ type Streaming = IsStream
145145-- Constructing a stream
146146------------------------------------------------------------------------------
147147
148−-- | Represesnts an empty stream just like @[]@ represents an empty list.
148+-- | An empty stream.
149+--
150+-- @
151+-- > toList nil
152+-- []
153+-- @
149154nil :: IsStream t => t m a
150155nil = fromStream S.nil
151156
157+-- | Create a singleton stream from a monadic action. Same as @m \`consM` nil@
158+-- but more efficient.
159+--
160+-- @
161+-- > toList $ once getLine
162+-- hello
163+-- ["hello"]
164+-- @
165+once :: (IsStream t, Monad m) => m a -> t m a
166+once = fromStream . S.once
167+
168+infixr 5 `consM`
169+
170+-- | Constructs a stream by adding a monadic action at the head of an existing
171+-- stream. For example:
172+--
173+-- @
174+-- > toList $ getLine \`consM` getLine \`consM` nil
175+-- hello
176+-- world
177+-- ["hello","world"]
178+-- @
179+consM :: (IsStream t, Monad m) => m a -> t m a -> t m a
180+consM m r = fromStream $ S.consM m (toStream r)
181+
182+infixr 5 |:
183+
184+-- | Operator equivalent of 'consM'.
185+--
186+-- @
187+-- > toList $ getLine |: getLine |: nil
188+-- hello
189+-- world
190+-- ["hello","world"]
191+-- @
192+(|:) :: (IsStream t, Monad m) => m a -> t m a -> t m a
193+(|:) = consM
194+
152195infixr 5 `cons`
153196
154−-- | Constructs a stream by adding a pure value at the head of an existing
155−-- stream, just like ':' constructs lists. For example:
197+-- | Construct a stream by adding a pure value at the head of an existing
198+-- stream. Same as @consM . return@. For example:
156199--
157200-- @
158−-- > let stream = 1 \`cons` 2 \`cons` 3 \`cons` nil
159−-- > (toList . streamly) stream
201+-- > toList $ 1 \`cons` 2 \`cons` 3 \`cons` nil
160202-- [1,2,3]
161203-- @
162204cons :: IsStream t => a -> t m a -> t m a
@@ -164,32 +206,13 @@ cons a r = fromStream $ S.cons a (toStream r)
164206
165207infixr 5 .:
166208
167−-- | Operator equivalent of 'cons' so that you can construct a stream of pure
168−-- values more succinctly like this:
209+-- | Operator equivalent of 'cons'.
169210--
170211-- @
171−-- > let stream = 1 .: 2 .: 3 .: nil
172−-- > (toList . streamly) stream
212+-- > toList $ 1 .: 2 .: 3 .: nil
173213-- [1,2,3]
174214-- @
175215--
176−-- '.:' constructs a stream just like ':' constructs a list.
177−--
178−-- Also note that another equivalent way of building streams from pure values
179−-- is:
180−--
181−-- @
182−-- > let stream = pure 1 <> pure 2 <> pure 3
183−-- > (toList . streamly) stream
184−-- [1,2,3]
185−-- @
186−--
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
191−-- latter.
192−--
193216(.:) :: IsStream t => a -> t m a -> t m a
194217(.:) = cons
195218
@@ -285,7 +308,7 @@ async m = do
285308
286309#define MONAD_APPLICATIVE_INSTANCE(STREAM,CONSTRAINT) \
287310instance (Monad m CONSTRAINT) => Applicative (STREAM m) where { \
288− pure = STREAM . singleton; \
311+ pure = STREAM . S.singleton; \
289312 (<*>) = ap }
290313
291314#define MONAD_COMMON_INSTANCES(STREAM,CONSTRAINT) \
@@ -413,7 +436,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
413436-- yielding one element from each stream alternately.
414437--
415438-- @
416−-- main = ('toList' . 'interleaving' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
439+-- main = ('toList' . 'costreamly' $ (fromFoldable [1,2]) \<\> (fromFoldable [3,4])) >>= print
417440-- @
418441-- @
419442-- [1,3,2,4]
@@ -424,7 +447,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
424447--
425448--
426449-- @
427−-- main = 'runStream' . 'interleaving' $ do
450+-- main = 'runStream' . 'costreamly' $ do
428451-- x <- return 1 \<\> return 2
429452-- y <- return 3 \<\> return 4
430453-- liftIO $ print (x, y)
@@ -436,7 +459,7 @@ MONAD_COMMON_INSTANCES(StreamT,)
436459-- (2,4)
437460-- @
438461--
439−-- Note that interleaving composition can only combine a finite number of
462+-- Note that costreamly composition can only combine a finite number of
440463-- streams as it needs to retain state for each unfinished stream.
441464--
442465newtype CostreamT m a = CostreamT {getCostreamT :: S.Stream m a}
@@ -899,15 +922,6 @@ type ZipStream a = ZipStreamM IO a
899922-- | An IO stream whose applicative instance zips streams parallely.
900923type ZipParallel a = ZipParallelM IO a
901924
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
904−-- 'ZipParallel'.
905−fromIO :: (IsStream t, MonadIO m) => IO a -> t m a
906−fromIO = streamly . liftIO
907−
908−fromM :: (IsStream t, Monad m) => m a -> t m a
909−fromM = streamly . lift
910−
911925------------------------------------------------------------------------------
912926-- Fold Utilities
913927------------------------------------------------------------------------------