on master help sign in
Add repeatM, consolidate other similar generation funcs
74a8969Harendra Kumar
src/Streamly/Prelude.hs+31−11
@@ -45,6 +45,7 @@ module Streamly.Prelude
45 -- generation function.
45 -- generation function.
46 , once
46 , once
47 , replicateM
47 , replicateM
48+ , repeatM
48 , iterate
49 , iterate
49 , iterateM
50 , iterateM
50 , fromFoldable
51 , fromFoldable
@@ -167,13 +168,41 @@ fromFoldable = Prelude.foldr cons nil
167each :: (IsStream t, Foldable f) => f a -> t m a
168each :: (IsStream t, Foldable f) => f a -> t m a
168each = fromFoldable
169each = fromFoldable
169
170
171+-- | Create a singleton stream by executing a monadic action once. Same as
172+-- @m \`consM` nil@ but more efficient.
173+--
174+-- @
175+-- > toList $ once getLine
176+-- hello
177+-- ["hello"]
178+-- @
179+once :: (IsStream t, Monad m) => m a -> t m a
180+once = fromStream . S.once
181+
182+-- | Generate a stream by performing a monadic action @n@ times.
183+replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
184+replicateM n m = fromStream $ go n
185+ where
186+ go cnt = Stream $ \_ stp _ yld ->
187+ if cnt <= 0
188+ then stp
189+ else m >>= \a -> yld a (go (cnt - 1))
190+
191+-- | Generate a stream by repeatedly executing a monadic action forever.
192+repeatM :: (IsStream t, Monad m) => m a -> t m a
193+repeatM = fromStream . go
194+ where
195+ go m = Stream $ \_ _ _ yld ->
196+ m >>= \a -> yld a (go m)
197+
170−-- | Iterate a pure function from a seed value, streaming the results forever
198+-- | Iterate a pure function from a seed value, streaming the results forever.
171iterate :: IsStream t => (a -> a) -> a -> t m a
199iterate :: IsStream t => (a -> a) -> a -> t m a
172iterate step = fromStream . go
200iterate step = fromStream . go
173 where
201 where
174 go s = S.cons s (go (step s))
202 go s = S.cons s (go (step s))
175
203
176−-- | Iterate a monadic function from a seed value, streaming the results forever
204+-- | Iterate a monadic function from a seed value, streaming the results
205+-- forever.
177iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
206iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
178iterateM step = fromStream . go
207iterateM step = fromStream . go
179 where
208 where
@@ -559,15 +588,6 @@ sequence m = fromStream $ go (toStream m)
559 yield ma r = ma >>= \b -> yld b (go r)
588 yield ma r = ma >>= \b -> yld b (go r)
560 in (S.runStream m1) Nothing stp single yield
589 in (S.runStream m1) Nothing stp single yield
561
590
562−-- | Generate a stream by performing an action @n@ times.
563−replicateM :: (IsStream t, Monad m) => Int -> m a -> t m a
564−replicateM n m = fromStream $ go n
565− where
566− go cnt = Stream $ \_ stp _ yld ->
567− if cnt <= 0
568− then stp
569− else m >>= \a -> yld a (go (cnt - 1))
570−
571------------------------------------------------------------------------------
591------------------------------------------------------------------------------
572-- Serially Zipping Streams
592-- Serially Zipping Streams
573------------------------------------------------------------------------------
593------------------------------------------------------------------------------
src/Streamly/Streams.hs−12
@@ -34,7 +34,6 @@ module Streamly.Streams
34
34
35 -- * Construction
35 -- * Construction
36 , nil
36 , nil
37− , once
38 , cons
37 , cons
39 , (.:)
38 , (.:)
40 , consM
39 , consM
@@ -148,17 +147,6 @@ type Streaming = IsStream
148nil :: IsStream t => t m a
147nil :: IsStream t => t m a
149nil = fromStream S.nil
148nil = fromStream S.nil
150
149
151−-- | Create a singleton stream from a monadic action. Same as @m \`consM` nil@
152−-- but more efficient.
153−--
154−-- @
155−-- > toList $ once getLine
156−-- hello
157−-- ["hello"]
158−-- @
159−once :: (IsStream t, Monad m) => m a -> t m a
160−once = fromStream . S.once
161−
162infixr 5 `consM`
150infixr 5 `consM`
163
151
164-- | Constructs a stream by adding a monadic action at the head of an existing
152-- | Constructs a stream by adding a monadic action at the head of an existing