on master help sign in
Add yield and repeat APIs
commit266148c84aac40f28ca30b8de0427cf291c99a67 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 23 Jun 2018, 07:35 (8 years ago) parent9c85811
4 files changed+39−31
Changelog.md+5 src/Streamly/Prelude.hs+5−3 src/Streamly/Streams/StreamK.hs+11−14 src/Streamly/Tutorial.hs+18−14
Changelog.md+5
@@ -4,6 +4,11 @@
44
55* `once` has been deprecated and renamed to `yieldM`
66
7+### Enhancements
8+
9+* Add `yield` to construct a singleton stream from a pure value
10+* Add `repeat` to generate an infinite stream by repeating a pure value
11+
712## 0.3.0
813
914### Breaking changes
src/Streamly/Prelude.hs+5−3
@@ -41,10 +41,12 @@ module Streamly.Prelude
4141 -- * Construction
4242 -- | Primitives to construct or inspect a stream.
4343 nil
44− , consM
45− , (|:)
44+ , K.yield
45+ , K.yieldM
4646 , cons
4747 , (.:)
48+ , consM
49+ , (|:)
4850
4951 -- * Generation by Unfolding
5052 , unfoldr
@@ -53,8 +55,8 @@ module Streamly.Prelude
5355 -- * Special Generation
5456 -- | Generate a monadic stream from an input structure, a seed or a
5557 -- generation function.
56− , K.yieldM
5758 , replicateM
59+ , K.repeat
5860 , repeatM
5961 , iterate
6062 , iterateM
src/Streamly/Streams/StreamK.hs+11−14
@@ -199,9 +199,14 @@ mkStream k = fromStream $ Stream $ \svr stp sng yld ->
199199nil :: IsStream t => t m a
200200nil = fromStream $ Stream $ \_ stp _ _ -> stp
201201
202−-- faster than yieldM because there is no bind.
203−-- | Create a singleton stream from a pure value. Same as @yieldM (return a)@
204−-- but more efficient.
202+-- Faster than yieldM because there is no bind. Usually we can construct a
203+-- stream from a pure value using "pure" in an applicative, however in case of
204+-- Zip streams pure creates an infinite stream.
205+-- | Create a singleton stream from a pure value. In monadic streams, 'pure' or
206+-- 'return' can be used in place of 'yield', however, in Zip applicative
207+-- streams 'pure' is equivalent to 'repeat'.
208+--
209+-- @since 0.4.0
205210yield :: IsStream t => a -> t m a
206211yield a = fromStream $ Stream $ \_ _ single _ -> single a
207212
@@ -257,17 +262,6 @@ infixr 5 .:
257262(.:) :: IsStream t => a -> t m a -> t m a
258263(.:) = cons
259264
260−-- | Constructs a stream by adding a monadic action at the head of an existing
261−-- stream. For example:
262−--
263−-- @
264−-- > toList $ getLine \`consM` getLine \`consM` nil
265−-- hello
266−-- world
267−-- ["hello","world"]
268−-- @
269−--
270−-- @since 0.2.0
271265{-# INLINE consMSerial #-}
272266consMSerial :: (Monad m) => m a -> Stream m a -> Stream m a
273267consMSerial m r = Stream $ \_ _ _ yld -> m >>= \a -> yld a r
@@ -336,6 +330,9 @@ runStream m = go (toStream m)
336330-- Special generation
337331-------------------------------------------------------------------------------
338332
333+-- | Generate an infinite stream by repeating a pure value.
334+--
335+-- @since 0.4.0
339336repeat :: IsStream t => a -> t m a
340337repeat a = let x = cons a x in x
341338
src/Streamly/Tutorial.hs+18−14
@@ -185,7 +185,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
185185-- represents a single IO action whereas the 'Serial' monad represents a series
186186-- of IO actions. The only change you need to make to go from 'IO' to 'Serial'
187187-- is to use 'runStream' to run the monad and to prefix the IO actions with
188−-- either 'once' or 'liftIO'. If you use liftIO you can switch from 'Serial'
188+-- either 'yieldM' or 'liftIO'. If you use liftIO you can switch from 'Serial'
189189-- to IO monad by simply removing the 'runStream' function; no other changes
190190-- are needed unless you have used some stream specific composition or
191191-- combinators.
@@ -347,13 +347,17 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
347347-- ["hello","world"]
348348-- @
349349--
350−-- To create a singleton stream from a pure value use 'pure' and to create a
351−-- singleton stream from a monadic action use 'once'.
350+-- To create a singleton stream from a pure value use 'yield' or 'pure' and to
351+-- create a singleton stream from a monadic action use 'yieldM'. Note that in
352+-- case of Zip applicative streams "pure" repeats the value to generate an
353+-- infinite stream.
352354--
353355-- @
354356-- > S.'toList' $ 'pure' 1
355357-- [1]
356−-- > S.'toList' $ S.'once' 'getLine'
358+-- > S.'toList' $ 'yield' 1
359+-- [1]
360+-- > S.'toList' $ S.'yieldM' 'getLine'
357361-- hello
358362-- ["hello"]
359363-- @
@@ -506,7 +510,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
506510-- seconds. After the delay it prints the number of seconds it slept.
507511--
508512-- @
509−-- delay n = S.'once' $ do
513+-- delay n = S.'yieldM' $ do
510514-- threadDelay (n * 1000000)
511515-- tid \<- myThreadId
512516-- putStrLn (show tid ++ ": Delay " ++ show n)
@@ -737,7 +741,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
737741--
738742-- @
739743-- main = 'runStream' . 'asyncly' $ traced (sqrt 9) '<>' traced (sqrt 16) '<>' traced (sqrt 25)
740−-- where traced m = S.'once' (myThreadId >>= print) >> return m
744+-- where traced m = S.'yieldM' (myThreadId >>= print) >> return m
741745-- @
742746-- @
743747-- ThreadId 40
@@ -859,7 +863,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
859863-- google = get "https://www.google.com/search?q=haskell"
860864-- bing = get "https://www.bing.com/search?q=haskell"
861865-- duckduckgo = get "https://www.duckduckgo.com/?q=haskell"
862−-- get s = S.'once' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
866+-- get s = S.'yieldM' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
863867-- @
864868--
865869-- The polymorphic version of the binary operation '<>' of the 'Parallel' type
@@ -918,7 +922,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
918922-- 'runStream' $ 'foldWith' 'async' (map delay [1..10])
919923-- 'runStream' $ 'foldMapWith' 'async' delay [1..10]
920924-- 'runStream' $ 'forEachWith' 'async' [1..10] delay
921−-- where delay n = S.'once' $ threadDelay (n * 1000000) >> print n
925+-- where delay n = S.'yieldM' $ threadDelay (n * 1000000) >> print n
922926-- @
923927
924928-- $nesting
@@ -988,7 +992,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
988992-- import "Streamly"
989993-- import qualified "Streamly.Prelude" as S
990994--
991−-- main = 'runStream' $ forever $ S.once getLine >>= S.once . putStrLn
995+-- main = 'runStream' $ forever $ S.yieldM getLine >>= S.yieldM . putStrLn
992996-- @
993997--
994998-- When multiple streams are composed using this style they nest in a DFS
@@ -1003,7 +1007,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
10031007-- main = 'runStream' $ do
10041008-- x <- S.'fromFoldable' [1,2]
10051009-- y <- S.'fromFoldable' [3,4]
1006−-- S.'once' $ putStrLn $ show (x, y)
1010+-- S.'yieldM' $ putStrLn $ show (x, y)
10071011-- @
10081012-- @
10091013-- (1,3)
@@ -1100,7 +1104,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
11001104-- main = 'runStream' . 'asyncly' $ do
11011105-- x <- S.'fromFoldable' [1,2]
11021106-- y <- S.'fromFoldable' [3,4]
1103−-- S.'once' $ putStrLn $ show (x, y)
1107+-- S.'yieldM' $ putStrLn $ show (x, y)
11041108-- @
11051109-- @
11061110-- (1,3)
@@ -1125,7 +1129,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
11251129-- main = 'runStream' . 'wSerially' $ do
11261130-- x <- S.'fromFoldable' [1,2]
11271131-- y <- S.'fromFoldable' [3,4]
1128−-- S.once $ putStrLn $ show (x, y)
1132+-- S.yieldM $ putStrLn $ show (x, y)
11291133-- @
11301134-- @
11311135-- (1,3)
@@ -1153,7 +1157,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
11531157-- main = 'runStream' . 'wAsyncly' $ do
11541158-- x <- S.'fromFoldable' [1,2]
11551159-- y <- S.'fromFoldable' [3,4]
1156−-- S.'once' $ putStrLn $ show (x, y)
1160+-- S.'yieldM' $ putStrLn $ show (x, y)
11571161-- @
11581162-- @
11591163-- (1,3)
@@ -1202,7 +1206,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
12021206-- sz <- sizes
12031207-- cl <- colors
12041208-- sh <- shapes
1205−-- S.'once' $ putStrLn $ show (sz, cl, sh)
1209+-- S.'yieldM' $ putStrLn $ show (sz, cl, sh)
12061210--
12071211-- where
12081212--