on master help sign in
Fix doctests in Streamly.Prelude
commit9c887919228b278dc57b03374e0e06371bf9140a authorHarendra Kumar <harendra@composewell.com> dateThu 1 Apr 2021, 02:50 (6 years ago) parent6173630
7 files changed+95−34
src/Streamly/Internal/Data/Stream/Ahead.hs+23−3 src/Streamly/Internal/Data/Stream/Async.hs+32−4 src/Streamly/Internal/Data/Stream/Parallel.hs+18−4 src/Streamly/Internal/Data/Stream/Serial.hs+2−1 src/Streamly/Internal/Data/Stream/StreamK/Type.hs+1 src/Streamly/Internal/Data/Stream/Zip.hs+7−8 src/Streamly/Prelude.hs+12−14
src/Streamly/Internal/Data/Stream/Ahead.hs+23−3
@@ -9,6 +9,16 @@
99-- Stability : experimental
1010-- Portability : GHC
1111--
12+-- To run examples in this module:
13+--
14+-- >>> import qualified Streamly.Prelude as Stream
15+-- >>> import Control.Concurrent (threadDelay)
16+-- >>> :{
17+-- delay n = do
18+-- threadDelay (n * 1000000) -- sleep for n seconds
19+-- putStrLn (show n ++ " sec") -- print "n sec"
20+-- return n -- IO Int
21+-- :}
1222--
1323module Streamly.Internal.Data.Stream.Ahead
1424 (
@@ -53,6 +63,15 @@ import Prelude hiding (map)
5363
5464#include "Instances.hs"
5565
66+-- $setup
67+-- >>> import Control.Concurrent (threadDelay)
68+-- >>> :{
69+-- delay n = do
70+-- threadDelay (n * 1000000) -- sleep for n seconds
71+-- putStrLn (show n ++ " sec") -- print "n sec"
72+-- return n -- IO Int
73+-- :}
74+
5675-------------------------------------------------------------------------------
5776-- Ahead
5877-------------------------------------------------------------------------------
@@ -600,9 +619,10 @@ infixr 6 `ahead`
600619-- original streams, side effects will happen in the order in which the streams
601620-- are evaluated:
602621--
603−-- >>> stream1 = Stream.yieldM (delay 4) -- SerialT IO Int
604−-- >>> stream2 = Stream.yieldM (delay 2) -- SerialT IO Int
605−-- >>> Stream.toList $ stream1 `ahead` stream2 -- IO [Int]
622+-- >>> import Streamly.Prelude (ahead)
623+-- >>> stream1 = Stream.yieldM (delay 4) :: SerialT IO Int
624+-- >>> stream2 = Stream.yieldM (delay 2) :: SerialT IO Int
625+-- >>> Stream.toList $ stream1 `ahead` stream2 :: IO [Int]
606626-- 2 sec
607627-- 4 sec
608628-- [4,2]
src/Streamly/Internal/Data/Stream/Async.hs+32−4
@@ -11,6 +11,16 @@
1111-- Stability : experimental
1212-- Portability : GHC
1313--
14+-- To run examples in this module:
15+--
16+-- >>> import qualified Streamly.Prelude as Stream
17+-- >>> import Control.Concurrent (threadDelay)
18+-- >>> :{
19+-- delay n = do
20+-- threadDelay (n * 1000000) -- sleep for n seconds
21+-- putStrLn (show n ++ " sec") -- print "n sec"
22+-- return n -- IO Int
23+-- :}
1424--
1525module Streamly.Internal.Data.Stream.Async
1626 (
@@ -61,6 +71,15 @@ import qualified Streamly.Internal.Data.Stream.StreamD.Type as D
6171
6272#include "Instances.hs"
6373
74+-- $setup
75+-- >>> import Control.Concurrent (threadDelay)
76+-- >>> :{
77+-- delay n = do
78+-- threadDelay (n * 1000000) -- sleep for n seconds
79+-- putStrLn (show n ++ " sec") -- print "n sec"
80+-- return n -- IO Int
81+-- :}
82+
6483-------------------------------------------------------------------------------
6584-- Async
6685-------------------------------------------------------------------------------
@@ -637,27 +656,33 @@ infixr 6 `async`
637656-- | Merges two streams, both the streams may be evaluated concurrently,
638657-- outputs from both are used as they arrive:
639658--
659+-- >>> import Streamly.Prelude (async)
640660-- >>> stream1 = Stream.yieldM (delay 4)
641661-- >>> stream2 = Stream.yieldM (delay 2)
642662-- >>> Stream.toList $ stream1 `async` stream2
643−-- [4,2]
663+-- 2 sec
664+-- 4 sec
665+-- [2,4]
644666--
645667-- Multiple streams can be combined. With enough threads, all of them can be
646668-- scheduled simultaneously:
647669--
648670-- >>> stream3 = Stream.yieldM (delay 1)
649671-- >>> Stream.toList $ stream1 `async` stream2 `async` stream3
672+-- ...
650673-- [1,2,4]
651674--
652675-- With 2 threads, only two can be scheduled at a time, when one of those
653676-- finishes, the third one gets scheduled:
654677--
655678-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3
679+-- ...
656680-- [2,1,4]
657681--
658682-- With a single thread, it becomes serial:
659683--
660684-- >>> Stream.toList $ Stream.maxThreads 1 $ stream1 `async` stream2 `async` stream3
685+-- ...
661686-- [4,2,1]
662687--
663688-- Only streams are scheduled for async evaluation, how actions within a
@@ -667,16 +692,18 @@ infixr 6 `async`
667692-- In the following example, both the streams are scheduled for concurrent
668693-- evaluation but each individual stream is evaluated serially:
669694--
670−-- >>> stream1 = Stream.fromListM $ map delay [3,3] -- SerialT IO Int
671−-- >>> stream2 = Stream.fromListM $ map delay [1,1] -- SerialT IO Int
695+-- >>> stream1 = Stream.fromListM $ Prelude.map delay [3,3] -- SerialT IO Int
696+-- >>> stream2 = Stream.fromListM $ Prelude.map delay [1,1] -- SerialT IO Int
672697-- >>> Stream.toList $ stream1 `async` stream2 -- IO [Int]
698+-- ...
673699-- [1,1,3,3]
674700--
675701-- If total threads are 2, the third stream is scheduled only after one of the
676702-- first two has finished:
677703--
678−-- >>> stream3 = Stream.fromListM $ map delay [2,2] -- SerialT IO Int
704+-- >>> stream3 = Stream.fromListM $ Prelude.map delay [2,2] -- SerialT IO Int
679705-- >>> Stream.toList $ Stream.maxThreads 2 $ stream1 `async` stream2 `async` stream3 -- IO [Int]
706+-- ...
680707-- [1,1,3,2,3,2]
681708--
682709-- Thus 'async' goes deep in first few streams rather than going wide in all
@@ -867,6 +894,7 @@ infixr 6 `wAsync`
867894-- With a single thread, 'async' starts behaving like 'serial' while 'wAsync'
868895-- starts behaving like 'wSerial'.
869896--
897+-- >>> import Streamly.Prelude (wAsync)
870898-- >>> stream1 = Stream.fromList [1,2,3]
871899-- >>> stream2 = Stream.fromList [4,5,6]
872900-- >>> Stream.toList $ Stream.asyncly $ Stream.maxThreads 1 $ stream1 `async` stream2
src/Streamly/Internal/Data/Stream/Parallel.hs+18−4
@@ -11,6 +11,16 @@
1111-- Stability : experimental
1212-- Portability : GHC
1313--
14+-- To run examples in this module:
15+--
16+-- >>> import qualified Streamly.Prelude as Stream
17+-- >>> import Control.Concurrent (threadDelay)
18+-- >>> :{
19+-- delay n = do
20+-- threadDelay (n * 1000000) -- sleep for n seconds
21+-- putStrLn (show n ++ " sec") -- print "n sec"
22+-- return n -- IO Int
23+-- :}
1424--
1525module Streamly.Internal.Data.Stream.Parallel
1626 (
@@ -74,10 +84,13 @@ import qualified Streamly.Internal.Data.Stream.SVar as SVar
7484
7585--
7686-- $setup
77−-- >>> :m
78−-- >>> import Prelude hiding (map)
79−-- >>> import qualified Streamly.Prelude as Stream
80−-- >>> import Streamly.Internal.Data.Stream.IsStream as Stream
87+-- >>> import Control.Concurrent (threadDelay)
88+-- >>> :{
89+-- delay n = do
90+-- threadDelay (n * 1000000) -- sleep for n seconds
91+-- putStrLn (show n ++ " sec") -- print "n sec"
92+-- return n -- IO Int
93+-- :}
8194
8295-------------------------------------------------------------------------------
8396-- Parallel
@@ -229,6 +242,7 @@ infixr 6 `parallel`
229242-- The only limit that applies to 'parallel' is 'Streamly.Prelude.maxBuffer'.
230243-- Evaluation may block if the output buffer becomes full.
231244--
245+-- >>> import Streamly.Prelude (parallel)
232246-- >>> stream = Stream.yieldM (delay 2) `parallel` Stream.yieldM (delay 1)
233247-- >>> Stream.toList stream -- IO [Int]
234248-- 1 sec
src/Streamly/Internal/Data/Stream/Serial.hs+2−1
@@ -281,7 +281,7 @@ TRAVERSABLE_INSTANCE(SerialT)
281281-- element @1@ in the first stream with all the nested iterations of element
282282-- @2@:
283283--
284−-- >>> Stream.fromList [(1,3),(1,4)] `wSerial` Stream.fromList [(2,3),(2,4)]
284+-- >>> Stream.toList $ Stream.fromList [(1,3),(1,4)] `wSerial` Stream.fromList [(2,3),(2,4)]
285285-- [(1,3),(2,3),(1,4),(2,4)]
286286--
287287-- The @W@ in the name stands for @wide@ or breadth wise scheduling in
@@ -355,6 +355,7 @@ infixr 6 `wSerial`
355355-- alternately. When one stream stops the rest of the other stream is used in
356356-- the output stream.
357357--
358+-- >>> import Streamly.Prelude (wSerial)
358359-- >>> stream1 = Stream.fromList [1,2]
359360-- >>> stream2 = Stream.fromList [3,4]
360361-- >>> Stream.toList $ Stream.wSerially $ stream1 `wSerial` stream2
src/Streamly/Internal/Data/Stream/StreamK/Type.hs+1
@@ -809,6 +809,7 @@ infixr 6 `serial`
809809-- | Appends two streams sequentially, yielding all elements from the first
810810-- stream, and then all elements from the second stream.
811811--
812+-- >>> import Streamly.Prelude (serial)
812813-- >>> stream1 = Stream.fromList [1,2]
813814-- >>> stream2 = Stream.fromList [3,4]
814815-- >>> Stream.toList $ stream1 `serial` stream2
src/Streamly/Internal/Data/Stream/Zip.hs+7−8
@@ -9,6 +9,9 @@
99-- Stability : experimental
1010-- Portability : GHC
1111--
12+-- To run examples in this module:
13+--
14+-- >>> import qualified Streamly.Prelude as Stream
1215--
1316module Streamly.Internal.Data.Stream.Zip
1417 (
@@ -121,15 +124,11 @@ zipAsyncWith f = zipAsyncWithM (\a b -> return (f a b))
121124-- i.e. it produces one element from each stream serially and then zips all
122125-- those elements.
123126--
124−-- @
125−-- main = (toList . 'zipSerially' $ (,,) \<$\> s1 \<*\> s2 \<*\> s3) >>= print
126−-- where s1 = fromFoldable [1, 2]
127−-- s2 = fromFoldable [3, 4]
128−-- s3 = fromFoldable [5, 6]
129−-- @
130−-- @
127+-- >>> s1 = Stream.fromFoldable [1, 2]
128+-- >>> s2 = Stream.fromFoldable [3, 4]
129+-- >>> s3 = Stream.fromFoldable [5, 6]
130+-- >>> Stream.toList $ Stream.zipSerially $ (,,) <$> s1 <*> s2 <*> s3
131131-- [(1,3,5),(2,4,6)]
132−-- @
133132--
134133-- The 'Semigroup' instance of this type works the same way as that of
135134-- 'SerialT'.
src/Streamly/Prelude.hs+12−14
@@ -29,7 +29,7 @@
2929-- which is more or less the same as a list:
3030--
3131-- >>> import Streamly.Prelude (SerialT, cons, consM, nil)
32−-- >>> stream :: SerialT IO Int = 1 `cons` 2 `cons` nil
32+-- >>> stream = 1 `cons` 2 `cons` nil :: SerialT IO Int
3333-- >>> Stream.toList stream -- IO [Int]
3434-- [1,2]
3535--
@@ -43,8 +43,10 @@
4343-- putStrLn (show n ++ " sec") -- print "n sec"
4444-- return n -- IO Int
4545-- :}
46+--
4647-- >>> delay 1
4748-- 1 sec
49+-- 1
4850--
4951-- 'consM' constructs a stream from effectful actions:
5052--
@@ -62,11 +64,15 @@
6264-- Finally, 'drain' folds the stream to IO discarding the () values, thus
6365-- producing only effects.
6466--
65−-- >>> :{
67+-- >>> import Data.Function ((&))
68+--
69+-- @
70+-- :{
6671-- Stream.repeatM getLine -- SerialT IO String
6772-- & Stream.mapM putStrLn -- SerialT IO ()
6873-- & Stream.drain -- IO ()
6974-- :}
75+-- @
7076--
7177-- This is a console echo program. It is an example of a declarative loop.
7278-- Compare it with an imperative @while@ loop.
@@ -84,7 +90,7 @@
8490-- 'SerialT' executes actions serially, so the total delay in the following
8591-- example is @2 + 1 = 3@ seconds:
8692--
87−-- >>> stream :: SerialT IO Int = delay 2 `consM` delay 1 `consM` nil
93+-- >>> stream = delay 2 `consM` delay 1 `consM` nil
8894-- >>> Stream.toList stream -- IO [Int]
8995-- 2 sec
9096-- 1 sec
@@ -93,8 +99,7 @@
9399-- 'AsyncT' executes the actions concurrently, so the total delay is @max 2 1 =
94100-- 2@ seconds:
95101--
96−-- >>> stream :: AsyncT IO Int = delay 2 `consM` delay 1 `consM` nil
97−-- >>> Stream.toList stream -- IO [Int]
102+-- >>> Stream.toList $ Stream.asyncly stream -- IO [Int]
98103-- 1 sec
99104-- 2 sec
100105-- [1,2]
@@ -106,8 +111,7 @@
106111-- 'AheadT' is similar to 'AsyncT' but the order of results is the same as the
107112-- order of actions, even though they execute concurrently:
108113--
109−-- >>> stream :: AheadT IO Int = delay 2 `consM` delay 1 `consM` nil
110−-- >>> Stream.toList stream -- IO [Int]
114+-- >>> Stream.toList $ Stream.aheadly stream -- IO [Int]
111115-- 1 sec
112116-- 2 sec
113117-- [2,1]
@@ -187,8 +191,6 @@
187191-- action from the first stream and then one action from the second stream and
188192-- so on:
189193--
190−-- >>> stream1 = Stream.fromListM [delay 1, delay 2]
191−-- >>> stream2 = Stream.fromListM [delay 3, delay 4]
192194-- >>> Stream.toList $ Stream.wSerially $ stream1 <> stream2
193195-- 1 sec
194196-- 3 sec
@@ -211,11 +213,7 @@
211213-- >>> stream1 = Stream.fromListM [delay 3, delay 4]
212214-- >>> stream2 = Stream.fromListM [delay 1, delay 2]
213215-- >>> Stream.toList $ stream1 `parallel` stream2
214−-- 1 sec
215−-- 2 sec
216−-- 3 sec
217−-- 4 sec
218−-- [1,2,3,4]
216+-- ...
219217--
220218-- We can use @concatMapWith parallel@ to read concurrently from all incoming
221219-- network connections and combine the input streams into a single output