on master help sign in
Fix examples in tutorial
6241794Harendra Kumar
src/Streamly/Tutorial.hs+36−4
@@ -182,6 +182,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
182--
182--
183-- @
183-- @
184-- import "Streamly"
184-- import "Streamly"
185+-- import "Streamly.Prelude"
185--
186--
186-- main = do
187-- main = do
187-- xs \<- 'toList' $ 'serially' $ return 1 <> return 2
188-- xs \<- 'toList' $ 'serially' $ return 1 <> return 2
@@ -369,6 +370,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
369--
370--
370-- @
371-- @
371-- import "Streamly"
372-- import "Streamly"
373+-- import "Streamly.Prelude"
372--
374--
373-- main = do
375-- main = do
374-- 'toList' . 'serially' $ 'foldWith' (<>) (map return [1..10]) >>= print
376-- 'toList' . 'serially' $ 'foldWith' (<>) (map return [1..10]) >>= print
@@ -408,6 +410,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
408-- list transformer like serial composition.
410-- list transformer like serial composition.
409--
411--
410-- @
412-- @
413+-- import "Streamly"
414+-- import "Streamly.Prelude"
415+--
411-- main = 'runStreamT' $ do
416-- main = 'runStreamT' $ do
412-- x <- 'each' [3,2,1]
417-- x <- 'each' [3,2,1]
413-- delay x
418-- delay x
@@ -423,12 +428,25 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
423-- same thread one after another. When compared to imperative programming, this
428-- same thread one after another. When compared to imperative programming, this
424-- can also be viewed as a @for@ loop with three iterations.
429-- can also be viewed as a @for@ loop with three iterations.
425--
430--
431+-- A console echo loop copying standard input to standard output can simply be
432+-- written like this:
433+--
434+-- @
435+-- import "Streamly"
436+-- import Data.Semigroup (cycle1)
437+--
438+-- main = 'runStreamT' $ cycle1 (liftIO getLine) >>= liftIO . putStrLn
439+-- @
440+--
426-- When multiple streams are composed using this style they nest in a DFS
441-- When multiple streams are composed using this style they nest in a DFS
427-- manner i.e. nested iterations of an iteration are executed before we proceed
442-- manner i.e. nested iterations of an iteration are executed before we proceed
428-- to the next iteration at higher level. This behaves just like nested @for@
443-- to the next iteration at higher level. This behaves just like nested @for@
429-- loops in imperative programming.
444-- loops in imperative programming.
430--
445--
431-- @
446-- @
447+-- import "Streamly"
448+-- import "Streamly.Prelude"
449+--
432-- main = 'runStreamT' $ do
450-- main = 'runStreamT' $ do
433-- x <- 'each' [1,2]
451-- x <- 'each' [1,2]
434-- y <- 'each' [3,4]
452-- y <- 'each' [3,4]
@@ -454,6 +472,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
454-- This is the concurrent version of 'StreamT'.
472-- This is the concurrent version of 'StreamT'.
455--
473--
456-- @
474-- @
475+-- import "Streamly"
476+-- import "Streamly.Prelude"
477+--
457-- main = 'runAsyncT' $ do
478-- main = 'runAsyncT' $ do
458-- x <- 'each' [3,2,1]
479-- x <- 'each' [3,2,1]
459-- delay x
480-- delay x
@@ -478,6 +499,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
478-- demand from the consumer.
499-- demand from the consumer.
479--
500--
480-- @
501-- @
502+-- import "Streamly"
503+-- import "Streamly.Prelude"
504+--
481-- main = 'runAsyncT' $ do
505-- main = 'runAsyncT' $ do
482-- x <- 'each' [1,2]
506-- x <- 'each' [1,2]
483-- y <- 'each' [3,4]
507-- y <- 'each' [3,4]
@@ -501,6 +525,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
501-- the continuations or iterations of the inner loop.
525-- the continuations or iterations of the inner loop.
502--
526--
503-- @
527-- @
528+-- import "Streamly"
529+-- import "Streamly.Prelude"
530+--
504-- main = 'runInterleavedT' $ do
531-- main = 'runInterleavedT' $ do
505-- x <- 'each' [1,2]
532-- x <- 'each' [1,2]
506-- y <- 'each' [3,4]
533-- y <- 'each' [3,4]
@@ -524,6 +551,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
524-- concurrently instead of the demand driven concurrency of 'AsyncT'.
551-- concurrently instead of the demand driven concurrency of 'AsyncT'.
525--
552--
526-- @
553-- @
554+-- import "Streamly"
555+-- import "Streamly.Prelude"
556+--
527-- main = 'runParallelT' $ do
557-- main = 'runParallelT' $ do
528-- x <- 'each' [3,2,1]
558-- x <- 'each' [3,2,1]
529-- delay x
559-- delay x
@@ -546,6 +576,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
546--
576--
547-- @
577-- @
548-- import "Streamly"
578-- import "Streamly"
579+-- import "Streamly.Prelude"
580+--
549--
581--
550-- composed :: 'Streaming' t => t m a
582-- composed :: 'Streaming' t => t m a
551-- composed = do
583-- composed = do
@@ -889,8 +921,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
889-- demonstrated below.
921-- demonstrated below.
890--
922--
891-- @
923-- @
892−-- import Streamly
924+-- import "Streamly"
893−-- import Streamly.Prelude
925+-- import "Streamly.Prelude"
894-- import System.IO (stdin)
926-- import System.IO (stdin)
895--
927--
896-- -- Adapt uncons to return an Either instead of Maybe
928-- -- Adapt uncons to return an Either instead of Maybe
@@ -911,7 +943,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
911-- -- pipe to streamly
943-- -- pipe to streamly
912-- -- Adapt P.next to return a Maybe instead of Either
944-- -- Adapt P.next to return a Maybe instead of Either
913-- let nextM p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
945-- let nextM p = P.next p >>= either (\\_ -> return Nothing) (return . Just)
914−-- runStreamT $ unfoldrM nextM P.stdinLn >>= lift . putStrLn
946+-- 'runStreamT' $ 'unfoldrM' nextM P.stdinLn >>= lift . putStrLn
915-- @
947-- @
916--
948--
917-- Interop with @streaming@:
949-- Interop with @streaming@:
@@ -925,7 +957,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
925-- S.stdoutLn $ S.unfoldr unconsE stdinLn
957-- S.stdoutLn $ S.unfoldr unconsE stdinLn
926--
958--
927-- -- streaming to streamly
959-- -- streaming to streamly
928−-- runStreamT $ unfoldrM S.uncons S.stdinLn >>= lift . putStrLn
960+-- 'runStreamT' $ unfoldrM S.uncons S.stdinLn >>= lift . putStrLn
929--
961--
930-- @
962-- @
931--
963--