on master help sign in
Update (minor) and reorg sections of the tutorial
9510577Harendra Kumar
src/Streamly/Tutorial.hs+51−34
@@ -19,9 +19,15 @@
19-- Streaming and concurrency together enable expressing reactive applications
19-- Streaming and concurrency together enable expressing reactive applications
20-- conveniently. See 'Streamly.Examples' for a simple SDL based FRP example.
20-- conveniently. See 'Streamly.Examples' for a simple SDL based FRP example.
21--
21--
22+-- Streamly streams are very much like the Haskell lists and most of the
23+-- functions that work on lists have a counterpart that works on streams.
24+-- However, streamly streams can be generated, consumed or combined
22−-- In this tutorial we will go over the basic concepts and how to use the
25+-- concurrently. In this tutorial we will go over the basic concepts and how to
26+-- use the library. The documentation of @Streamly@ module has more details on
27+-- core APIs. For more APIs for constructing, folding, filtering, mapping and
28+-- zipping etc. see the documentation of @Streamly.Prelude@ module. For
23−-- library. For examples and other ways to use the library see
29+-- examples and other ways to use the library see the module
24−-- the module @Streamly.Examples@ as well.
30+-- @Streamly.Examples@ as well.
25
31
26module Streamly.Tutorial
32module Streamly.Tutorial
27 (
33 (
@@ -34,28 +40,28 @@ module Streamly.Tutorial
34 -- ** Eliminating Streams
40 -- ** Eliminating Streams
35 -- $eliminating
41 -- $eliminating
36
42
37− -- * Folding Streams
43+ -- * Combining Streams
38− -- $folding
44+ -- $combining
39
45
40− -- * Semigroup Style Composition
46+ -- ** Semigroup Style
41 -- $semigroup
47 -- $semigroup
42
48
43− -- ** Serial composition ('<>')
49+ -- *** Serial composition ('<>')
44 -- $serial
50 -- $serial
45
51
46− -- ** Async composition ('<|')
52+ -- *** Async composition ('<|')
47 -- $parallel
53 -- $parallel
48
54
49− -- ** Interleaved composition ('<=>')
55+ -- *** Interleaved composition ('<=>')
50 -- $interleaved
56 -- $interleaved
51
57
52− -- ** Parallel composition ('<|>')
58+ -- *** Fair Concurrent composition ('<|>')
53 -- $fairParallel
59 -- $fairParallel
54
60
55− -- ** Custom composition
61+ -- *** Custom composition
56 -- $custom
62 -- $custom
57
63
58− -- * Monoid Style composition
64+ -- ** Monoid Style
59 -- $monoid
65 -- $monoid
60
66
61 -- * Transforming Streams
67 -- * Transforming Streams
@@ -64,16 +70,16 @@ module Streamly.Tutorial
64 -- ** Monad
70 -- ** Monad
65 -- $monad
71 -- $monad
66
72
67− -- *** Serial Iterations ('StreamT')
73+ -- *** Serial Composition ('StreamT')
68 -- $regularSerial
74 -- $regularSerial
69
75
70− -- *** Concurrent Iterations ('AsyncT')
76+ -- *** Async Composition ('AsyncT')
71 -- $concurrentNesting
77 -- $concurrentNesting
72
78
73− -- *** Interleaved Iterations ('InterleavedT')
79+ -- *** Interleaved Composition ('InterleavedT')
74 -- $interleavedNesting
80 -- $interleavedNesting
75
81
76− -- *** Fairly Concurrent Iterations ('ParallelT')
82+ -- *** Fair Concurrent Composition ('ParallelT')
77 -- $fairlyConcurrentNesting
83 -- $fairlyConcurrentNesting
78
84
79 -- *** Exercise
85 -- *** Exercise
@@ -85,9 +91,6 @@ module Streamly.Tutorial
85 -- ** Functor
91 -- ** Functor
86 -- $functor
92 -- $functor
87
93
88− -- * Summary of Compositions
89− -- $compositionSummary
90−
91 -- * Zipping Streams
94 -- * Zipping Streams
92 -- $zipping
95 -- $zipping
93
96
@@ -97,6 +100,9 @@ module Streamly.Tutorial
97 -- ** Parallel Zipping
100 -- ** Parallel Zipping
98 -- $parallelzip
101 -- $parallelzip
99
102
103+ -- * Summary of Compositions
104+ -- $compositionSummary
105+
100 -- * Concurrent Programming Examples
106 -- * Concurrent Programming Examples
101 -- $concurrent
107 -- $concurrent
102
108
@@ -153,6 +159,9 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
153-- @
159-- @
154-- return 1 <> return 2 <> return 3 :: StreamT IO Int
160-- return 1 <> return 2 <> return 3 :: StreamT IO Int
155-- @
161-- @
162+--
163+-- For more ways to construct or generate a stream see the module
164+-- @Streamly.Prelude@.
156
165
157-- $eliminating
166-- $eliminating
158--
167--
@@ -181,7 +190,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
181-- print xs
190-- print xs
182-- @
191-- @
183--
192--
184−-- For other ways to eliminate a stream see the module @Streamly.Prelude@.
193+-- For other ways to eliminate or fold a stream see the module
194+-- @Streamly.Prelude@.
185
195
186-- $semigroup
196-- $semigroup
187-- Streams of the same type can be combined into a composite stream in many
197-- Streams of the same type can be combined into a composite stream in many
@@ -577,20 +587,29 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
577
587
578-- $functor
588-- $functor
579--
589--
580−-- The simplest transformation on a stream is mapping a function on
590+-- 'fmap' transforms a stream by mapping a function on all elements of the
581−-- all elements of the stream using 'fmap'.
591+-- stream. The functor instance of each stream type defines 'fmap' to be
592+-- precisely the same as 'liftM', and therefore 'fmap' is always serial
593+-- irrespective of the type. For concurrent mapping, alternative versions of
594+-- 'fmap', namely, 'asyncMap' and 'parMap' are provided.
582--
595--
583-- @
596-- @
584-- import Streamly
597-- import Streamly
585--
598--
586-- main = (toList $ serially $ fmap show $ each [1..10]) >>= print
599-- main = (toList $ serially $ fmap show $ each [1..10]) >>= print
587-- @
600-- @
601+--
602+-- Also see the 'mapM' and 'sequence' functions for mapping actions, in the
603+-- @Streamly.Prelude@ module.
588
604
589-- $applicative
605-- $applicative
590--
606--
591−-- Applicative is a special case of monad and behaves just like the monadic
607+-- Applicative is precisely the same as the 'ap' operation of 'Monad'. For
608+-- zipping and parallel applicatives separate types 'ZipStream' and 'ZipAsync'
609+-- are provided.
610+--
592−-- composition. The following example runs all iterations serially and takes a
611+-- The following example runs all iterations serially and takes a total 17
593−-- total 17 seconds (1 + 3 + 4 + 2 + 3 + 4):
612+-- seconds (1 + 3 + 4 + 2 + 3 + 4):
594--
613--
595-- @
614-- @
596-- import Streamly
615-- import Streamly
@@ -661,9 +680,6 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
661-- [(1,3),(2,3),(1,4),(2,4)]
680-- [(1,3),(2,3),(1,4),(2,4)]
662-- @
681-- @
663
682
664−-- $folding
665−-- Folds
666−
667-- $compositionSummary
683-- $compositionSummary
668--
684--
669-- The following table summarizes the types for monadic compositions and the
685-- The following table summarizes the types for monadic compositions and the
@@ -690,11 +706,12 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
690-- streams are combined together using a zip function producing a new
706-- streams are combined together using a zip function producing a new
691-- stream of outputs. Two different types are provided for serial and
707-- stream of outputs. Two different types are provided for serial and
692-- concurrent zipping. These types provide an applicative instance that zips
708-- concurrent zipping. These types provide an applicative instance that zips
693−-- the argument streams.
709+-- the argument streams. Also see the zipping function in the @Streamly.Prelude@
710+-- module.
694
711
695-- $serialzip
712-- $serialzip
696--
713--
697−-- 'StreamZip' zips streams serially:
714+-- 'ZipStream' zips streams serially:
698--
715--
699-- @
716-- @
700-- import Streamly
717-- import Streamly
@@ -721,7 +738,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
721
738
722-- $parallelzip
739-- $parallelzip
723--
740--
724−-- 'AsyncZip' zips streams concurrently:
741+-- 'ZipAsync' zips streams concurrently:
725--
742--
726-- @
743-- @
727-- import Streamly
744-- import Streamly
@@ -764,7 +781,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
764-- import Data.List (sum)
781-- import Data.List (sum)
765--
782--
766-- main = do
783-- main = do
767−-- squares \<- toList $ serially $ foldMapWith (<|) (\x -\> return $ x * x) [1..100]
784+-- squares \<- toList $ serially $ forEachWith (<|) [1..100] $ \\x -\> return $ x * x
768-- print $ sum squares
785-- print $ sum squares
769-- @
786-- @
770--
787--
@@ -779,8 +796,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
779--
796--
780-- main = do
797-- main = do
781-- z \<- toList $ asyncly $ do
798-- z \<- toList $ asyncly $ do
782−-- xsq \<- foldMapWith (\<|) (\\x -> return $ x * x) [1..100]
799+-- xsq \<- forEachWith (\<|) [1..100] $ \\x -> return $ x * x
783−-- ysq \<- foldMapWith (\<|) (\\x -> return $ x * x) [1..100]
800+-- ysq \<- forEachWith (\<|) [1..100] $ \\x -> return $ x * x
784-- return $ sqrt (xsq + ysq)
801-- return $ sqrt (xsq + ysq)
785-- print $ sum z
802-- print $ sum z
786-- @
803-- @
streamly.cabal+2−1
@@ -42,7 +42,8 @@ description: Streamly unifies concurrency and streaming in a single monad
42 > liftIO $ mapM_ putStrLn $ map show files
42 > liftIO $ mapM_ putStrLn $ map show files
43 > foldMapWith (<|>) readdir dirs
43 > foldMapWith (<|>) readdir dirs
44 .
44 .
45− See "Streamly.Tutorial" and "Streamly.Examples" for more details.
45+ See "Streamly.Tutorial" and "Streamly.Examples" (available only when built
46+ with 'examples' build flag) for more details.
46 .
47 .
47 This library was originally inspired by the `transient`
48 This library was originally inspired by the `transient`
48 package authored by Alberto G. Corona.
49 package authored by Alberto G. Corona.