on master help sign in
Add repeatM, consolidate other similar generation funcs
commit74a8969df73c050d716221a0993099d40c45982c authorHarendra Kumar <harendra.kumar@gmail.com> dateThu 3 May 2018, 04:36 (8 years ago) parent69f22f4
2 files changed+31−23
src/Streamly/Prelude.hs+31−11 src/Streamly/Streams.hs−12
src/Streamly/Prelude.hs+31−11
@@ -45,6 +45,7 @@ module Streamly.Prelude
4545 -- generation function.
4646 , once
4747 , replicateM
48+ , repeatM
4849 , iterate
4950 , iterateM
5051 , fromFoldable
@@ -167,13 +168,41 @@ fromFoldable = Prelude.foldr cons nil
167168each :: (IsStream t, Foldable f) => f a -> t m a
168169each = fromFoldable
169170
170−-- | Iterate a pure function from a seed value, streaming the results forever
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+
198+-- | Iterate a pure function from a seed value, streaming the results forever.
171199iterate :: IsStream t => (a -> a) -> a -> t m a
172200iterate step = fromStream . go
173201 where
174202 go s = S.cons s (go (step s))
175203
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.
177206iterateM :: (IsStream t, Monad m) => (a -> m a) -> a -> t m a
178207iterateM step = fromStream . go
179208 where
@@ -559,15 +588,6 @@ sequence m = fromStream $ go (toStream m)
559588 yield ma r = ma >>= \b -> yld b (go r)
560589 in (S.runStream m1) Nothing stp single yield
561590
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−
571591------------------------------------------------------------------------------
572592-- Serially Zipping Streams
573593------------------------------------------------------------------------------
src/Streamly/Streams.hs−12
@@ -34,7 +34,6 @@ module Streamly.Streams
3434
3535 -- * Construction
3636 , nil
37− , once
3837 , cons
3938 , (.:)
4039 , consM
@@ -148,17 +147,6 @@ type Streaming = IsStream
148147nil :: IsStream t => t m a
149148nil = fromStream S.nil
150149
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−
162150infixr 5 `consM`
163151
164152-- | Constructs a stream by adding a monadic action at the head of an existing