on master help sign in
Reflect some code changes in the tutorial
f8f9b2cHarendra Kumar
src/Streamly/Tutorial.hs+40−42
@@ -150,17 +150,19 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
150
150
151-- $eliminating
151-- $eliminating
152--
152--
153−-- 'runAsyncly' runs composed actions in the 'AsyncT' monad, lowering the type
153+-- 'runStreaming' runs composed actions in a streaming monad, lowering the type
154-- into the underlying monad and discarding the values generated by the stream;
154-- into the underlying monad and discarding the values generated by the stream;
155-- 'toList' collects the resulting stream in a list. 'uncons' can be used
155-- 'toList' collects the resulting stream in a list. 'uncons' can be used
156−-- to deconstruct an 'AsyncT' stream into head and tail.
156+-- to deconstruct a stream into head and tail. Before you run a stream you need
157+-- to express the type of the stream by using one of the streaming type
158+-- combinators ('serially', 'asyncly', 'parallely' etc.).
157--
159--
158-- @
160-- @
159-- import Streamly
161-- import Streamly
160--
162--
161-- main = do
163-- main = do
162−-- runAsyncly $ liftIO $ putStrLn "Hello world!"
164+-- runStreaming $ serially $ liftIO $ putStrLn "Hello world!"
163−-- xs \<- toList $ return 1 <> return 2
165+-- xs \<- toList $ serially $ return 1 <> return 2
164-- print xs
166-- print xs
165-- @
167-- @
166
168
@@ -191,7 +193,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
191-- prints the sequence 3, 2, 1 and takes a total of 6 seconds:
193-- prints the sequence 3, 2, 1 and takes a total of 6 seconds:
192--
194--
193-- @
195-- @
194−-- main = runAsyncly $ timer 3 <> timer 2 <> timer 1
196+-- main = runStreaming $ serially $ timer 3 <> timer 2 <> timer 1
195-- @
197-- @
196-- @
198-- @
197-- ThreadId 36: Timer 3
199-- ThreadId 36: Timer 3
@@ -205,7 +207,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
205-- following example should print the sequence 1, 3, 2, 4:
207-- following example should print the sequence 1, 3, 2, 4:
206--
208--
207-- @
209-- @
208−-- main = runAsyncly $ (timer 1 <> timer 2) \<=> (timer 3 <> timer 4)
210+-- main = runStreaming $ serially $ (timer 1 <> timer 2) \<=> (timer 3 <> timer 4)
209-- @
211-- @
210-- @
212-- @
211-- ThreadId 36: Timer 1
213-- ThreadId 36: Timer 1
@@ -232,7 +234,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
232-- computation to keep up even if none of them blocks:
234-- computation to keep up even if none of them blocks:
233--
235--
234-- @
236-- @
235−-- main = runAsyncly $ traced (sqrt 9) <| traced (sqrt 16) <| traced (sqrt 25)
237+-- main = runStreaming $ serially $ traced (sqrt 9) <| traced (sqrt 16) <| traced (sqrt 25)
236-- @
238-- @
237-- @
239-- @
238-- ThreadId 40
240-- ThreadId 40
@@ -244,7 +246,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
244-- next one in a separate thread and so on:
246-- next one in a separate thread and so on:
245--
247--
246-- @
248-- @
247−-- main = runAsyncly $ timer 3 <| timer 2 <| timer 1
249+-- main = runStreaming $ serially $ timer 3 <| timer 2 <| timer 1
248-- @
250-- @
249-- @
251-- @
250-- ThreadId 42: Timer 1
252-- ThreadId 42: Timer 1
@@ -255,7 +257,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
255-- When we have a hierarchy of computations this operator follows DFS style:
257-- When we have a hierarchy of computations this operator follows DFS style:
256--
258--
257-- @
259-- @
258−-- main = runAsyncly $ (p 1 <| p 2) <| (p 3 <| p 4)
260+-- main = runStreaming $ serially $ (p 1 <| p 2) <| (p 3 <| p 4)
259-- where p = liftIO . print
261-- where p = liftIO . print
260-- @
262-- @
261-- @
263-- @
@@ -292,7 +294,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
292-- import Streamly
294-- import Streamly
293-- import Network.HTTP.Simple
295-- import Network.HTTP.Simple
294--
296--
295−-- main = runAsyncly $ google \<|> bing \<|> duckduckgo
297+-- main = runStreaming $ serially $ google \<|> bing \<|> duckduckgo
296-- where
298-- where
297-- google = get "https://www.google.com/search?q=haskell"
299-- google = get "https://www.google.com/search?q=haskell"
298-- bing = get "https://www.bing.com/search?q=haskell"
300-- bing = get "https://www.bing.com/search?q=haskell"
@@ -327,11 +329,11 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
327-- import Streamly
329-- import Streamly
328--
330--
329-- main = do
331-- main = do
330−-- runAsyncly $ liftIO $ putStrLn "Hello world!"
332+-- runStreaming $ serially $ liftIO $ putStrLn "Hello world!"
331−-- xs \<- toList $ return 1 <> return 2; print xs
333+-- xs \<- toList . serially $ return 1 <> return 2; print xs
332−-- xs \<- toList $ foldWith (<>) (map return [1..10]); print xs
334+-- xs \<- toList . serially $ foldWith (<>) (map return [1..10]); print xs
333−-- xs \<- toList $ foldMapWith (<>) return [1..10]; print xs
335+-- xs \<- toList . serially $ foldMapWith (<>) return [1..10]; print xs
334−-- xs \<- toList $ forEachWith (<>) [1..10] return; print xs
336+-- xs \<- toList . serially $ forEachWith (<>) [1..10] return; print xs
335-- @
337-- @
336
338
337-- $transforming
339-- $transforming
@@ -348,7 +350,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
348
350
349-- $functor
351-- $functor
350--
352--
351−-- The simplest way to transform an 'AsyncT' stream is by mapping a function on
353+-- The simplest way to transform a stream is by mapping a function on
352-- all elements of the stream using 'fmap'.
354-- all elements of the stream using 'fmap'.
353--
355--
354-- @
356-- @
@@ -357,7 +359,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
357-- main = do
359-- main = do
358-- let nums = foldMapWith (<>) return [1..10]
360-- let nums = foldMapWith (<>) return [1..10]
359-- let strings = fmap show nums
361-- let strings = fmap show nums
360−-- (toList $ fmap putStrLn strings) >>= sequence
362+-- (toList $ serially $ fmap putStrLn strings) >>= sequence
361-- @
363-- @
362
364
363-- $applicative
365-- $applicative
@@ -374,7 +376,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
374-- The following example prints all combinations of size, color and shape:
376-- The following example prints all combinations of size, color and shape:
375--
377--
376-- @
378-- @
377−-- main = (toList $ (,,) \<$> sizes \<*> colors \<*> shapes) >>= print
379+-- main = (toList . serially $ (,,) \<$> sizes \<*> colors \<*> shapes) >>= print
378--
380--
379-- where
381-- where
380--
382--
@@ -398,7 +400,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
398-- import Streamly
400-- import Streamly
399-- import Data.Semigroup (cycle1)
401-- import Data.Semigroup (cycle1)
400--
402--
401−-- main = runAsyncly $ do
403+-- main = runStreaming $ serially $ do
402-- str <- cycle1 (liftIO getLine)
404-- str <- cycle1 (liftIO getLine)
403-- liftIO $ putStrLn str
405-- liftIO $ putStrLn str
404-- @
406-- @
@@ -408,7 +410,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
408-- combinations of size, color and shape.
410-- combinations of size, color and shape.
409--
411--
410-- @
412-- @
411−-- main = runAsyncly $ do
413+-- main = runStreaming $ serially $ do
412-- sz <- sizes
414-- sz <- sizes
413-- cl <- colors
415-- cl <- colors
414-- sh <- shapes
416-- sh <- shapes
@@ -425,17 +427,13 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
425
427
426-- $interleavedNesting
428-- $interleavedNesting
427--
429--
428−-- Instead of the regular bind operation (>>=) we can use the interleaved bind
429−-- operator (>->) to interleave the iterations of the nested loops. For
430−-- example:
431−--
432-- @
430-- @
433-- import Streamly
431-- import Streamly
434--
432--
435−-- main = runAsyncly $
433+-- main = runStreaming $ interleaved $
436−-- sizes >-> \\sz ->
434+-- sz <- sizes
437−-- colors >-> \\cl ->
435+-- cl <- colors
438−-- shapes >-> \\sh ->
436+-- sh <- shapes
439-- liftIO $ putStrLn $ show (sz, cl, sh)
437-- liftIO $ putStrLn $ show (sz, cl, sh)
440--
438--
441-- where
439-- where
@@ -458,12 +456,12 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
458-- tid <- myThreadId
456-- tid <- myThreadId
459-- putStrLn (show tid ++ ": " ++ s)
457-- putStrLn (show tid ++ ": " ++ s)
460--
458--
461−-- main = runAsyncly $ do
459+-- main = runStreaming $ asyncly $ do
462-- liftIO $ hSetBuffering stdout LineBuffering
460-- liftIO $ hSetBuffering stdout LineBuffering
463−-- sizes >>| \\sz ->
461+-- sz <- sizes
464−-- colors >>| \\cl ->
462+-- cl <- colors
465−-- shapes >>| \\sh ->
463+-- sh <- shapes
466−-- liftIO $ printWithTid $ show (sz, cl, sh)
464+-- liftIO $ putStrLn $ show (sz, cl, sh)
467--
465--
468-- where
466-- where
469--
467--
@@ -485,12 +483,12 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
485-- tid <- myThreadId
483-- tid <- myThreadId
486-- putStrLn (show tid ++ ": " ++ s)
484-- putStrLn (show tid ++ ": " ++ s)
487--
485--
488−-- main = runAsyncly $ do
486+-- main = runStreaming $ parallely $ do
489-- liftIO $ hSetBuffering stdout LineBuffering
487-- liftIO $ hSetBuffering stdout LineBuffering
490−-- sizes >|> \\sz ->
488+-- sz <- sizes
491−-- colors >|> \\cl ->
489+-- cl <- colors
492−-- shapes >|> \\sh ->
490+-- sh <- shapes
493−-- liftIO $ printWithTid $ show (sz, cl, sh)
491+-- liftIO $ putStrLn $ show (sz, cl, sh)
494--
492--
495-- where
493-- where
496--
494--
@@ -520,7 +518,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
520-- import Data.List (sum)
518-- import Data.List (sum)
521--
519--
522-- main = do
520-- main = do
523−-- squares \<- toList $ foldMapWith (<|) (\x -\> return $ x * x) [1..100]
521+-- squares \<- toList $ serially $ foldMapWith (<|) (\x -\> return $ x * x) [1..100]
524-- print $ sum squares
522-- print $ sum squares
525-- @
523-- @
526--
524--
@@ -533,8 +531,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
533-- import Data.List (sum)
531-- import Data.List (sum)
534--
532--
535-- main = do
533-- main = do
536−-- z \<- toList $ foldMapWith (\<|) (\\x -> return $ x * x) [1..100] >>| \\xsq ->
534+-- z \<- toList $ asyncly $ foldMapWith (\<|) (\\x -> return $ x * x) [1..100] >>= \\xsq ->
537−-- foldMapWith (\<|) (\\x -> return $ x * x) [1..100] >>| \\ysq ->
535+-- foldMapWith (\<|) (\\x -> return $ x * x) [1..100] >>= \\ysq ->
538-- return $ sqrt (xsq + ysq)
536-- return $ sqrt (xsq + ysq)
539-- print $ sum z
537-- print $ sum z
540-- @
538-- @
@@ -580,7 +578,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
580-- main = do
578-- main = do
581-- putStrLn "Your health is deteriorating due to acid rain,\
579-- putStrLn "Your health is deteriorating due to acid rain,\
582-- \ type \"potion\" or \"quit\""
580-- \ type \"potion\" or \"quit\""
583−-- runStateT (runAsyncly game) 60
581+-- runStateT (runStreaming $ serially $ game) 60
584-- @
582-- @
585
583
586-- $statemachine
584-- $statemachine