@@ -10,6 +10,7 @@ module Streamly.Internal.Data.Stream.Generate
@@ -21,27 +22,30 @@ import Data.Semigroup (Semigroup(..))
21import Data.Semigroup (Semigroup(..))
22import Data.Semigroup (Semigroup(..))
23import Streamly.Internal.Data.Stream.Type (Stream)
24import Streamly.Internal.Data.Stream.Type (Stream)
25+import Streamly.Internal.Data.Unfold (Unfold)
25import qualified Streamly.Internal.Data.Stream.Type as Stream
27import qualified Streamly.Internal.Data.Stream.Type as Stream
28+import qualified Streamly.Internal.Data.Stream.StreamD.Type as StreamD
26import qualified Streamly.Internal.Data.Stream.StreamK.Type as StreamK
29import qualified Streamly.Internal.Data.Stream.StreamK.Type as StreamK
30−-- >>> import Control.Concurrent (threadDelay)
33+-- >>> import qualified Streamly.Data.Fold as Fold
31−-- >>> import Data.IORef
34+-- >>> import qualified Streamly.Data.Unfold as Unfold
32−-- >>> import Prelude hiding (zipWith, concatMap, concat)
33-- >>> import qualified Streamly.Internal.Data.Stream as Stream
35-- >>> import qualified Streamly.Internal.Data.Stream as Stream
36-- | Construct a stream by adding a pure value at the head of an existing
39-- | Construct a stream by adding a pure value at the head of an existing
37−-- stream. This is the same as @(return a) \`consM` r@ but
40+-- stream. Same as the following but more efficient:
38−-- more efficient. For concurrent streams this is not concurrent whereas
41+--
39−-- 'consM' is concurrent. For example:
42+-- For example:
41−-- @
44+-- >> s = 1 `Stream.cons` 2 `Stream.cons` 3 `Stream.cons` Stream.nil
42−-- > toList $ 1 \`cons` 2 \`cons` 3 \`cons` nil
45+-- >> Stream.fold Fold.toList s
43−-- [1,2,3]
46+-- >[1,2,3]
48+-- >>> cons x xs = return x `Stream.consM` xs
46-- /Pre-release/
50-- /Pre-release/
@@ -49,17 +53,16 @@ import qualified Streamly.Internal.Data.Stream.StreamK.Type as StreamK
49cons :: a -> Stream m a -> Stream m a
53cons :: a -> Stream m a -> Stream m a
50cons x = Stream.fromStreamK . StreamK.cons x . Stream.toStreamK
54cons x = Stream.fromStreamK . StreamK.cons x . Stream.toStreamK
52-- | Constructs a stream by adding a monadic action at the head of an
58-- | Constructs a stream by adding a monadic action at the head of an
53-- existing stream. For example:
59-- existing stream. For example:
55−-- @
61+-- >> s = putChar 'h' `Stream.consM` putChar 'i' `Stream.consM` Stream.nil
56−-- > toList $ getLine \`consM` getLine \`consM` nil
62+-- >> Stream.fold Fold.toList s
62−-- > consM x xs = fromEffect x `append` xs
65+-- >> consM x xs = Stream.fromEffect x `Stream.append` xs
64-- /Pre-release/
67-- /Pre-release/
@@ -68,17 +71,35 @@ cons x = Stream.fromStreamK . StreamK.cons x . Stream.toStreamK
68consM :: Monad m => m a -> Stream m a -> Stream m a
71consM :: Monad m => m a -> Stream m a -> Stream m a
69consM m = Stream.fromStreamK . StreamK.consM m . Stream.toStreamK
72consM m = Stream.fromStreamK . StreamK.consM m . Stream.toStreamK
74+------------------------------------------------------------------------------
76+------------------------------------------------------------------------------
78+-- | Convert an 'Unfold' into a stream by supplying it an input seed.
80+-- >>> s = Stream.unfold (Unfold.replicateM 3) (putStrLn "hello")
81+-- >>> Stream.fold Fold.drain s
88+unfold :: Monad m => Unfold m a b -> a -> Stream m b
89+unfold unf = Stream.fromStreamD . StreamD.unfold unf
71------------------------------------------------------------------------------
91------------------------------------------------------------------------------
72-- Combining
92-- Combining
73------------------------------------------------------------------------------
93------------------------------------------------------------------------------
75-- | Appends two streams sequentially, yielding all elements from the first
97-- | Appends two streams sequentially, yielding all elements from the first
76-- stream, and then all elements from the second stream.
98-- stream, and then all elements from the second stream.
78−-- >>> import Streamly.Internal.Data.Stream (append)
79−-- >>> stream1 = Stream.fromList [1,2]
100+-- >>> s1 = Stream.unfold Unfold.fromList [1,2]
80−-- >>> stream2 = Stream.fromList [3,4]
101+-- >>> s2 = Stream.unfold Unfold.fromList [3,4]
81−-- >>> Stream.toList $ stream1 `append` stream2
102+-- >>> Stream.fold Fold.toList $ s1 `Stream.append` s2
82-- [1,2,3,4]
103-- [1,2,3,4]
84-- This operation can be used to fold an infinite lazy container of streams.
105-- This operation can be used to fold an infinite lazy container of streams.