@@ -47,21 +47,20 @@ module Streamly.Tutorial
47 -- $transformation
47 -- $transformation
49 -- * Merging Streams
49 -- * Merging Streams
52 -- ** Semigroup Style
51 -- ** Semigroup Style
53 -- $semigroup
52 -- $semigroup
55− -- *** Serial Composition ('streamly')
54+ -- *** Serial Depth First Composition ('Stream')
56 -- $serial
55 -- $serial
58− -- *** Serial Interleaved Composition ('costreamly')
57+ -- *** Serial Breadth First Composition ('Costream')
59 -- $interleaved
58 -- $interleaved
61− -- *** Left Biased Concurrent Composition ('coparallely')
60+ -- *** Concurrent Depth First Composition ('ParAhead')
62− -- $coparallel
61+ -- $parAhead
64− -- *** Round Robin Concurrent Composition ('parallely')
63+ -- *** Concurrent Breadth First Composition ('CoParAhead')
65 -- $parallel
64 -- $parallel
67 -- *** Custom composition
66 -- *** Custom composition
@@ -76,16 +75,16 @@ module Streamly.Tutorial
76 -- ** Monad
75 -- ** Monad
79− -- *** Serial Nesting ('streamly')
78+ -- *** Serial Depth First Nesting ('Stream')
80 -- $regularSerial
79 -- $regularSerial
82− -- *** Serial Interleaved Nesting ('costreamly')
81+ -- *** Serial Breadth First Nesting ('Costream')
83 -- $interleavedNesting
82 -- $interleavedNesting
85− -- *** Left Biased Concurrent Nesting ('coparallely')
84+ -- *** Concurrent Depth First Nesting ('ParAhead')
86 -- $concurrentNesting
85 -- $concurrentNesting
88− -- *** Round Robin Concurrent Nesting ('parallely')
87+ -- *** Concurrent Breadth First Nesting ('CoparAhead')
89 -- $fairlyConcurrentNesting
88 -- $fairlyConcurrentNesting
91 -- *** Exercise
90 -- *** Exercise
@@ -156,7 +155,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
156-- in fact a synonym for @StreamT IO@. There are a few more types similar to
155-- in fact a synonym for @StreamT IO@. There are a few more types similar to
157-- 'StreamT' that all represent streams and differ only in the 'Semigroup',
156-- 'StreamT' that all represent streams and differ only in the 'Semigroup',
158-- 'Applicative' and 'Monad' compositions of the streams. 'Stream' and
157-- 'Applicative' and 'Monad' compositions of the streams. 'Stream' and
159−-- 'Costream' types compose serially whereas 'Coparallel' and 'Parallel' types
158+-- 'Costream' types compose serially whereas 'ParAhead' and 'CoparAhead' types
160-- compose concurrently. All these types can be freely inter-converted using
159-- compose concurrently. All these types can be freely inter-converted using
161-- type combinators without any cost. You can freely switch to any type of
160-- type combinators without any cost. You can freely switch to any type of
162-- composition at any point in the program. When no type annotation or
161-- composition at any point in the program. When no type annotation or
@@ -190,7 +189,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
190-- @StreamT IO@.
189-- @StreamT IO@.
192-- Similarly we have monad transformer types for other stream types as well viz.
191-- Similarly we have monad transformer types for other stream types as well viz.
193−-- 'CostreamT', 'CoparallelT' and 'ParallelT'.
192+-- 'CostreamT', 'ParAheadT' and 'CoparAheadT'.
195-- To lift a value from an underlying monad in a monad transformer stack into a
194-- To lift a value from an underlying monad in a monad transformer stack into a
196-- singleton stream use 'lift' and to lift from an IO action use 'liftIO'.
195-- singleton stream use 'lift' and to lift from an IO action use 'liftIO'.
@@ -309,13 +308,13 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
309-- operation '<>'. Streams can be combined in many different ways as described
308-- operation '<>'. Streams can be combined in many different ways as described
310-- in the following sections, the '<>' operation behaves differently depending
309-- in the following sections, the '<>' operation behaves differently depending
311-- on the stream type in effect. Streamly streams can be of many
310-- on the stream type in effect. Streamly streams can be of many
312−-- different types i.e. 'Stream', 'Costream', 'Coparallel', 'Parallel',
311+-- different types i.e. 'Stream', 'Costream', 'ParAhead', 'CoparAhead',
313-- 'ZipStream', and 'ZipParallel'. The first four are monads whereas the last
312-- 'ZipStream', and 'ZipParallel'. The first four are monads whereas the last
314-- two are applicatives. These types can be freely converted from one to
313-- two are applicatives. These types can be freely converted from one to
315-- another to acheive the desired composition style. To force a particular
314-- another to acheive the desired composition style. To force a particular
316-- type of composition we coerce the stream type using the corresponding type
315-- type of composition we coerce the stream type using the corresponding type
317−-- adapting combinator from 'streamly', 'costreamly', 'coparallely',
316+-- adapting combinator from 'asStream', 'asCostream', 'asParAhead',
318−-- 'parallely', 'zipStreamly' or 'zipParallely'. The default stream type is
317+-- 'asCoparAhead', 'asZipStream' or 'asZipParallel'. The default stream type is
319-- 'Stream' unless you change it by using one of the combinators.
318-- 'Stream' unless you change it by using one of the combinators.
321-- To illustrate the concurrent vs serial composition aspects, we will use the
320-- To illustrate the concurrent vs serial composition aspects, we will use the
@@ -336,18 +335,20 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
336-- $serial
335-- $serial
338-- The 'Semigroup' operation '<>' of the 'Stream' type combines the two streams
337-- The 'Semigroup' operation '<>' of the 'Stream' type combines the two streams
339−-- in a serial manner. We use the 'streamly' type combinator to effect 'Stream'
338+-- in a /serial depth first/ manner. We use the 'asStream' type combinator to
340−-- style of composition. We can also use the 'Stream' type annotation for the
339+-- effect 'Stream' style of composition. We can also use an explicit 'Stream'
341−-- stream type to acheive the same effect. However, since 'Stream' is the
340+-- type annotation for the stream to acheive the same effect. However, since
342−-- default type unless explicitly specified by using a combinator, we can
341+-- 'Stream' is the default type unless explicitly specified by using a
343−-- omit using an explicit combinator or type annotation for this style of
342+-- combinator, we can omit using an explicit combinator or type annotation for
344−-- composition.
343+-- this style of composition.
346-- When two streams with multiple elements are combined in this manner, the
345-- When two streams with multiple elements are combined in this manner, the
347-- monadic actions in the two streams are performed sequentially i.e. first all
346-- monadic actions in the two streams are performed sequentially i.e. first all
348-- actions in the first stream are performed sequentially and then all actions
347-- actions in the first stream are performed sequentially and then all actions
349−-- in the second stream are performed sequentially. The following example
348+-- in the second stream are performed sequentially. We call it
349+-- /serial depth first/ as the full depth of one stream is fully traversed
350−-- prints the sequence 1, 2, 3, 4:
350+-- before we move to the next. The following example prints the sequence 1, 2,
353-- main = 'runStream' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
354-- main = 'runStream' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
@@ -383,18 +384,18 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
383-- $interleaved
384-- $interleaved
385-- The 'Semigroup' operation '<>' of the 'Costream' type combines the two
386-- The 'Semigroup' operation '<>' of the 'Costream' type combines the two
386−-- streams in a serial interleaved manner. We use the 'costreamly' type
387+-- streams in a /serial breadth first/ manner. We use the 'asCostream' type
387-- combinator to effect 'Costream' style of composition. We can also use the
388-- combinator to effect 'Costream' style of composition. We can also use the
388−-- 'Costream' type annotation for the stream type to acheive the same effect.
389+-- 'Costream' type annotation for the stream to acheive the same effect.
390−-- When two streams with multiple elements are combined in this manner, the
391+-- When two streams with multiple elements are combined in this manner, we
391−-- monadic actions in the two streams are performed in an interleaved manner
392+-- traverse all the streams in a breadth first manner i.e. one action from each
392−-- i.e. one action from the first stream is performed and its output yielded
393+-- stream is peformed and yielded to the resulting stream before we come back
393−-- and then one action from the second stream is performed and its output
394+-- to the first stream again and so on.
394−-- yielded, and so on. The following example prints the sequence 1, 3, 2, 4
395+-- The following example prints the sequence 1, 3, 2, 4
397−-- main = 'runStream' . 'costreamly' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
398+-- main = 'runStream' . 'asCostream' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
@@ -409,7 +410,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
409-- thread and take a combined total of 6 seconds:
410-- thread and take a combined total of 6 seconds:
412−-- main = 'runStream' . 'costreamly' $ delay 3 <> delay 2 <> delay 1
413+-- main = 'runStream' . 'asCostream' $ delay 3 <> delay 2 <> delay 1
415-- ThreadId 36: Delay 3
416-- ThreadId 36: Delay 3
@@ -419,7 +420,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
420-- The polymorphic version of the 'Costream' binary operation '<>' is called
421-- The polymorphic version of the 'Costream' binary operation '<>' is called
421-- 'cosplice'. We can use 'cosplice' to join streams in an interleaved manner
422-- 'cosplice'. We can use 'cosplice' to join streams in an interleaved manner
422−-- irrespective of the type, notice that we have not used the 'costreamly'
423+-- irrespective of the type, notice that we have not used the 'asCostream'
423-- combinator in the following example:
424-- combinator in the following example:
@@ -435,28 +436,27 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
435-- Note that this composition cannot be used to fold infinite number of streams
436-- Note that this composition cannot be used to fold infinite number of streams
436-- since it requires preserving the state until a stream is finished.
437-- since it requires preserving the state until a stream is finished.
438−-- $coparallel
439+-- $parAhead
440−-- The 'Semigroup' operation '<>' of the 'Coparallel' type combines the two
441+-- The 'Semigroup' operation '<>' of the 'ParAhead' type combines the two
441−-- streams in a left biased concurrent manner. We use the 'coparallel' type
442+-- streams in a depth first manner with parallel look ahead. We use the
442−-- combinator to effect 'Coparallel' style of composition. We can also use the
443+-- 'asParAhead' type combinator to effect 'ParAhead' style of composition. We
443−-- 'Coparallel' type annotation for the stream type to acheive the same effect.
444+-- can also use the 'ParAhead' type annotation for the stream type to acheive
445-- When two streams with multiple elements are combined in this manner, the
447-- When two streams with multiple elements are combined in this manner, the
446−-- monadic actions in the two streams can be performed concurrently
448+-- streams are traversed in depth first manner just like 'Stream', however it
447−-- /when needed/ with a bias towards the left stream i.e. actions from the left
449+-- can execute the next stream concurrently and return the results from it
450+-- as they arrive i.e. the results from the next stream may be yielded even
451+-- before the results from the first stream. Concurrent execution of the next
448−-- stream are performed sequentially, if the left stream blocks or cannot
452+-- stream(s) is performed if the first stream blocks or if it cannot produce
449−-- generate enough output to keep the consumer of the stream busy then it can
453+-- output at the rate that is enough to meet the consumer demand. Multiple
450−-- execute actions from the right stream concurrently. The outputs of the
454+-- streams can be executed concurrently to meet the demand.
451−-- concurrent actions are yielded by the composite stream in the order in which
452−-- they complete. This can be thought of as the concurrent analogue of the
453−-- 'Stream' style serial composition. If concurrent execution is not trigerred
454−-- by consumer demand then it would execute actions exactly in the same order
455−-- as 'Stream'. In the following example the first stream does not block,
455+-- In the following example the first stream does not block,
456-- therefore the first stream is completely exhausted before the second.
456-- therefore the first stream is completely exhausted before the second.
459−-- main = 'runStream' . 'coparallely' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
459+-- main = 'runStream' . 'asParAhead' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
@@ -470,7 +470,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
470-- and 3 would be yielded first and then 2 and 4 would be yielded.
470-- and 3 would be yielded first and then 2 and 4 would be yielded.
473−-- main = 'runStream' . 'coparallely' $ (p 1 |: p 2 |: nil) <> (p 3 |: p 4 |: nil)
473+-- main = 'runStream' . 'asParAhead' $ (p 1 |: p 2 |: nil) <> (p 3 |: p 4 |: nil)
474-- where p n = threadDelay 1000000 >> print n
474-- where p n = threadDelay 1000000 >> print n
@@ -485,7 +485,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
485-- shortest delay is printed first:
485-- shortest delay is printed first:
488−-- main = 'runStream' . 'coparallely' $ delay 3 '<>' delay 2 '<>' delay 1
488+-- main = 'runStream' . 'asParAhead' $ delay 3 '<>' delay 2 '<>' delay 1
491-- ThreadId 42: Delay 1
491-- ThreadId 42: Delay 1
@@ -504,7 +504,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
504-- second as all of the actions are concurrent.
504-- second as all of the actions are concurrent.
507−-- main = 'runStream' . 'coparallely' $ (delay 1 <> delay 2) <> (delay 3 <> delay 4)
507+-- main = 'runStream' . 'asParAhead' $ (delay 1 <> delay 2) <> (delay 3 <> delay 4)
@@ -521,7 +521,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
521-- even if none of them blocks:
521-- even if none of them blocks:
524−-- main = 'runStream' . 'coparallely' $ traced (sqrt 9) '<>' traced (sqrt 16) '<>' traced (sqrt 25)
524+-- main = 'runStream' . 'asParAhead' $ traced (sqrt 9) '<>' traced (sqrt 16) '<>' traced (sqrt 25)
525-- where traced m = S.'once' (myThreadId >>= print) >> return m
525-- where traced m = S.'once' (myThreadId >>= print) >> return m
@@ -533,13 +533,13 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
533-- Note that the order of printing in the above examples may change due to
533-- Note that the order of printing in the above examples may change due to
534-- variations in scheduling latencies for concurrent threads.
534-- variations in scheduling latencies for concurrent threads.
536−-- The polymorphic version of the 'Coparallel' binary operation '<>' is called
536+-- The polymorphic version of the 'ParAhead' binary operation '<>' is called
537−-- 'coparallel'. We can use 'coparallel' to join streams in a left biased
537+-- 'parAhead'. We can use 'parAhead' to join streams in a left biased
538-- adaptively concurrent manner irrespective of the type, notice that we have
538-- adaptively concurrent manner irrespective of the type, notice that we have
539−-- not used the 'coparallely' combinator in the following example:
539+-- not used the 'asParAhead' combinator in the following example:
542−-- main = 'runStream' $ delay 3 \`coparallel` delay 2 \`coparallel` delay 1
542+-- main = 'runStream' $ delay 3 \`parAhead` delay 2 \`parAhead` delay 1
545-- ThreadId 42: Delay 1
545-- ThreadId 42: Delay 1
@@ -555,12 +555,12 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
555-- all computations at once for any reason 'Parallel' style must be used
555-- all computations at once for any reason 'Parallel' style must be used
556-- instead.
556-- instead.
558−-- 'Coparallel' style should be preferred over 'Parallel' unless you really
558+-- 'ParAhead' style should be preferred over 'CoparAhead' unless you really
559−-- need 'Parallel'. It utilizes the resources optimally. It should be used when
559+-- need 'CoparAhead'. It utilizes the resources optimally. It should be used when
560-- we know that the computations can run in parallel but we do not care if they
560-- we know that the computations can run in parallel but we do not care if they
561-- actually run in parallel or not, that decision can be left to the scheduler
561-- actually run in parallel or not, that decision can be left to the scheduler
562-- based on demand. Also, note that this operator can be used to fold infinite
562-- based on demand. Also, note that this operator can be used to fold infinite
563−-- number of streams in contrast to the 'Parallel' style, because it does not
563+-- number of streams in contrast to the 'CoparAhead' style, because it does not
564-- require us to run all of them at the same time in a fair manner.
564-- require us to run all of them at the same time in a fair manner.
566-- $parallel
566-- $parallel
@@ -586,7 +586,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
586-- import qualified Streamly.Prelude as S
586-- import qualified Streamly.Prelude as S
587-- import Network.HTTP.Simple
587-- import Network.HTTP.Simple
589−-- main = 'runStream' . 'parallely' $ google \<> bing \<> duckduckgo
589+-- main = 'runStream' . 'asParallel' $ google \<> bing \<> duckduckgo
591-- google = get "https://www.google.com/search?q=haskell"
591-- google = get "https://www.google.com/search?q=haskell"
592-- bing = get "https://www.bing.com/search?q=haskell"
592-- bing = get "https://www.bing.com/search?q=haskell"
@@ -594,12 +594,12 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
594-- get s = S.'once' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
594-- get s = S.'once' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
597−-- In the following example we can see that the fairly parallel composition
597+-- In the following example we can see that the co-ahead parallel composition
598-- yields outputs in the same order as the 'Costream' composition, though this
598-- yields outputs in the same order as the 'Costream' composition, though this
599-- is not always guaranteed because of thread scheduling variances.
599-- is not always guaranteed because of thread scheduling variances.
602−-- main = 'runStream' . 'parallely' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
602+-- main = 'runStream' . 'asCoparAhead' $ (print 1 |: print 2 |: nil) <> (print 3 |: print 4 |: nil)
@@ -608,13 +608,13 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
611−-- The polymorphic version of the 'Parallel' binary operation '<>' is called
611+-- The polymorphic version of the 'CoparAhead' binary operation '<>' is called
612−-- 'parallel'. We can use 'parallel' to join streams in a fairly
612+-- 'coparAhead'. We can use 'coparAhead' to join streams in a fairly
613-- concurrent manner irrespective of the type, notice that we have
613-- concurrent manner irrespective of the type, notice that we have
614−-- not used the 'parallely' combinator in the following example:
614+-- not used the 'asCoparAhead' combinator in the following example:
617−-- main = 'runStream' $ delay 3 \`parallel` delay 2 \`parallel` delay 1
617+-- main = 'runStream' $ delay 3 \`coparAhead` delay 2 \`coparAhead` delay 1
620-- ThreadId 42: Delay 1
620-- ThreadId 42: Delay 1
@@ -635,7 +635,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
635-- time. Take an example of a merge sort of two sorted streams. We need to
635-- time. Take an example of a merge sort of two sorted streams. We need to
636-- keep consuming items from the stream which has the lowest item in the sort
636-- keep consuming items from the stream which has the lowest item in the sort
637-- order. This can be achieved using async references to streams. See
637-- order. This can be achieved using async references to streams. See
638−-- "Streamly.Examples.MergeSortedStreams".
638+-- "MergeSort.hs" in the examples directory.
640-- $monoid
640-- $monoid
@@ -660,10 +660,10 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
660-- import Control.Concurrent
660-- import Control.Concurrent
662-- main = do
662-- main = do
663−-- 'runStream' $ 'parallely' $ foldMap delay [1..10]
663+-- 'runStream' $ 'asParAhead' $ foldMap delay [1..10]
664−-- 'runStream' $ 'foldWith' 'parallel' (map delay [1..10])
664+-- 'runStream' $ 'foldWith' 'parAhead' (map delay [1..10])
665−-- 'runStream' $ 'foldMapWith' 'parallel' delay [1..10]
665+-- 'runStream' $ 'foldMapWith' 'parAhead' delay [1..10]
666−-- 'runStream' $ 'forEachWith' 'parallel' [1..10] delay
666+-- 'runStream' $ 'forEachWith' 'parAhead' [1..10] delay
667-- where delay n = S.'once' $ threadDelay (n * 1000000) >> print n
667-- where delay n = S.'once' $ threadDelay (n * 1000000) >> print n
@@ -688,7 +688,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
688-- element in the first stream and for each element in the second stream
688-- element in the first stream and for each element in the second stream
689-- execute the body of the loop.
689-- execute the body of the loop.
691−-- The 'Monad' instances of 'Stream', 'Costream', 'Coparallel' and 'Parallel'
691+-- The 'Monad' instances of 'Stream', 'Costream', 'ParAhead' and 'CoparAhead'
692-- stream types support different flavors of nested looping. In other words,
692-- stream types support different flavors of nested looping. In other words,
693-- they are all variants of list transformer. The nesting behavior of these
693-- they are all variants of list transformer. The nesting behavior of these
694-- types exactly correspond to the way they merge streams as we discussed in
694-- types exactly correspond to the way they merge streams as we discussed in
@@ -763,22 +763,22 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
764-- $concurrentNesting
764-- $concurrentNesting
766−-- The 'Monad' composition of 'Coparallel' type can perform the iterations of a
766+-- The 'Monad' composition of 'ParAhead' type can perform the iterations of a
767-- loop concurrently. Concurrency is demand driven i.e. more concurrent
767-- loop concurrently. Concurrency is demand driven i.e. more concurrent
768-- iterations are started only if the previous iterations are not able to
768-- iterations are started only if the previous iterations are not able to
769-- produce enough output for the consumer of the output stream. This works
769-- produce enough output for the consumer of the output stream. This works
770−-- exactly the same way as the merging of two streams 'coparallely' works.
770+-- exactly the same way as the merging of two streams 'asParAhead' works.
771-- This is the concurrent analogue of 'Stream' style monadic composition.
771-- This is the concurrent analogue of 'Stream' style monadic composition.
773−-- The 'coparallely' type combinator can be used to switch to this
773+-- The 'asParAhead' type combinator can be used to switch to this
774-- style of composition. Alternatively, a type annotation can be used to
774-- style of composition. Alternatively, a type annotation can be used to
775−-- specify the type of the stream as 'Coparallel'.
775+-- specify the type of the stream as 'ParAhead'.
778-- import "Streamly"
778-- import "Streamly"
779-- import "Streamly.Prelude"
779-- import "Streamly.Prelude"
781−-- main = 'runStream' . 'coparallely' $ do
781+-- main = 'runStream' . 'asParAhead' $ do
782-- x <- 'fromFoldable' [3,2,1]
782-- x <- 'fromFoldable' [3,2,1]
783-- delay x
783-- delay x
@@ -794,7 +794,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
794-- When compared to imperative programming, this can be viewed as a @for@ loop
794-- When compared to imperative programming, this can be viewed as a @for@ loop
795-- with three concurrent iterations.
795-- with three concurrent iterations.
797−-- Concurrency is demand driven just as in the case of 'coparallel' merging.
797+-- Concurrency is demand driven just as in the case of 'parAhead' merging.
798-- When multiple streams are composed using this style, the iterations are
798-- When multiple streams are composed using this style, the iterations are
799-- triggered in a DFS manner just like 'Stream' i.e. nested iterations are
799-- triggered in a DFS manner just like 'Stream' i.e. nested iterations are
800-- executed before we proceed to the next iteration at higher level. However,
800-- executed before we proceed to the next iteration at higher level. However,
@@ -805,7 +805,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
805-- import "Streamly"
805-- import "Streamly"
806-- import qualified "Streamly.Prelude" as S
806-- import qualified "Streamly.Prelude" as S
808−-- main = 'runStream' . 'coparallely' $ do
808+-- main = 'runStream' . 'asParAhead' $ do
809-- x <- S.'fromFoldable' [1,2]
809-- x <- S.'fromFoldable' [1,2]
810-- y <- S.'fromFoldable' [3,4]
810-- y <- S.'fromFoldable' [3,4]
811-- S.'once' $ putStrLn $ show (x, y)
811-- S.'once' $ putStrLn $ show (x, y)
@@ -821,8 +821,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
822-- The 'Monad' composition of 'Costream' type interleaves the iterations of
822-- The 'Monad' composition of 'Costream' type interleaves the iterations of
823-- outer and inner loops in a nested loop composition. This works exactly the
823-- outer and inner loops in a nested loop composition. This works exactly the
824−-- same way as the merging of two streams in 'costreamly' fashion works. The
824+-- same way as the merging of two streams in 'asCostream' fashion works. The
825−-- 'costreamly' type combinator can be used to switch to this style of
825+-- 'asCostream' type combinator can be used to switch to this style of
826-- composition. Alternatively, a type annotation can be used to specify the
826-- composition. Alternatively, a type annotation can be used to specify the
827-- type of the stream as 'Costream'.
827-- type of the stream as 'Costream'.
@@ -830,7 +830,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
830-- import "Streamly"
830-- import "Streamly"
831-- import qualified "Streamly.Prelude" as S
831-- import qualified "Streamly.Prelude" as S
833−-- main = 'runStream' . 'costreamly' $ do
833+-- main = 'runStream' . 'asCostream' $ do
834-- x <- S.'fromFoldable' [1,2]
834-- x <- S.'fromFoldable' [1,2]
835-- y <- S.'fromFoldable' [3,4]
835-- y <- S.'fromFoldable' [3,4]
836-- S.once $ putStrLn $ show (x, y)
836-- S.once $ putStrLn $ show (x, y)
@@ -846,7 +846,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
846-- $fairlyConcurrentNesting
846-- $fairlyConcurrentNesting
848-- The 'Monad' composition of 'Parallel' type starts all the iterations of a
848-- The 'Monad' composition of 'Parallel' type starts all the iterations of a
849−-- loop concurrently instead of demand driven as in the case of 'Coparallel'.
849+-- loop concurrently instead of demand driven as in the case of 'ParAhead'.
850-- This works exactly the same way as the merging streams 'parallely' works.
850-- This works exactly the same way as the merging streams 'parallely' works.
851-- The 'parallely' type combinator can be used to switch to this style of
851-- The 'parallely' type combinator can be used to switch to this style of
852-- composition. Alternatively, a type annotation can be used to specify the
852-- composition. Alternatively, a type annotation can be used to specify the
@@ -894,10 +894,10 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
894-- Now we can interpret this in whatever way we want:
894-- Now we can interpret this in whatever way we want:
897−-- main = 'runStream' . 'streamly' $ composed
897+-- main = 'runStream' . 'asStream' $ composed
898−-- main = 'runStream' . 'costreamly' $ composed
898+-- main = 'runStream' . 'asCostream' $ composed
899−-- main = 'runStream' . 'coparallely' $ composed
899+-- main = 'runStream' . 'asParAhead' $ composed
900−-- main = 'runStream' . 'parallely' $ composed
900+-- main = 'runStream' . 'asCoparAhead' $ composed
903-- As an exercise try to figure out the output of this code for each mode of
903-- As an exercise try to figure out the output of this code for each mode of
@@ -937,7 +937,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
937-- s2 = d 3 <> d 4
937-- s2 = d 3 <> d 4
938-- d n = delay n >> return n
938-- d n = delay n >> return n
940−-- main = (S.'toList' . 'streamly' $ (,) \<$> s1 \<*> s2) >>= print
940+-- main = (S.'toList' . 'asStream' $ (,) \<$> s1 \<*> s2) >>= print
943-- ThreadId 36: Delay 1
943-- ThreadId 36: Delay 1
@@ -953,7 +953,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
953-- order but since it is serial it takes a total of 17 seconds:
953-- order but since it is serial it takes a total of 17 seconds:
956−-- main = (S.'toList' . 'costreamly' $ (,) \<$> s1 \<*> s2) >>= print
956+-- main = (S.'toList' . 'asCostream' $ (,) \<$> s1 \<*> s2) >>= print
959-- ThreadId 36: Delay 1
959-- ThreadId 36: Delay 1
@@ -965,11 +965,11 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
965-- [(1,3),(2,3),(1,4),(2,4)]
965-- [(1,3),(2,3),(1,4),(2,4)]
968−-- 'Coparallel' can run the iterations concurrently and therefore takes a total
968+-- 'ParAhead' can run the iterations concurrently and therefore takes a total
969-- of 10 seconds (1 + 2 + 3 + 4):
969-- of 10 seconds (1 + 2 + 3 + 4):
972−-- main = (S.'toList' . 'coparallely' $ (,) \<$> s1 \<*> s2) >>= print
972+-- main = (S.'toList' . 'asParAhead' $ (,) \<$> s1 \<*> s2) >>= print
975-- ThreadId 34: Delay 1
975-- ThreadId 34: Delay 1
@@ -981,11 +981,11 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
981-- [(1,3),(2,3),(1,4),(2,4)]
981-- [(1,3),(2,3),(1,4),(2,4)]
984−-- Similalrly 'Parallel' as well can run the iterations concurrently and
984+-- Similalrly 'CoparAhead' as well can run the iterations concurrently and
985-- therefore takes a total of 10 seconds (1 + 2 + 3 + 4):
985-- therefore takes a total of 10 seconds (1 + 2 + 3 + 4):
988−-- main = (S.'toList' . 'parallely' $ (,) \<$> s1 \<*> s2) >>= print
988+-- main = (S.'toList' . 'asCoparAhead' $ (,) \<$> s1 \<*> s2) >>= print
991-- ThreadId 34: Delay 1
991-- ThreadId 34: Delay 1
@@ -999,21 +999,16 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1000-- $compositionSummary
1000-- $compositionSummary
1002−-- The following table summarizes the types for monadic compositions and the
1002+-- The following table summarizes the types for monadic and sum style
1003−-- operators for sum style compositions. This table captures the essence of
1003+-- compositions.
1007-- +-----+--------------+--------------+
1006-- +-----+--------------+--------------+
1008-- | | Serial | Concurrent |
1007-- | | Serial | Concurrent |
1009-- +=====+==============+==============+
1008-- +=====+==============+==============+
1010−-- | DFS | 'Stream' | 'Coparallel' |
1009+-- | DFS | 'Stream' | 'ParAhead' |
1011−-- | +--------------+--------------+
1012−-- | | 'splice' | 'coparallel' |
1013-- +-----+--------------+--------------+
1010-- +-----+--------------+--------------+
1014−-- | BFS | 'Costream' | 'Parallel' |
1011+-- | BFS | 'Costream' | 'CoparAhead' |
1015−-- | +--------------+--------------+
1016−-- | | 'cosplice' | 'parallel' |
1017-- +-----+--------------+--------------+
1012-- +-----+--------------+--------------+
@@ -1029,7 +1024,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1029-- $serialzip
1024-- $serialzip
1031-- The applicative instance of 'ZipStream' type zips streams serially.
1026-- The applicative instance of 'ZipStream' type zips streams serially.
1032−-- 'zipStreamly' type combinator can be used to switch to serial applicative
1027+-- 'asZipStream' type combinator can be used to switch to serial applicative
1033-- zip composition:
1028-- zip composition:
@@ -1041,7 +1036,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1041-- s1 = 'streamly' $ d 1 <> d 2
1036-- s1 = 'streamly' $ d 1 <> d 2
1042-- s2 = 'streamly' $ d 3 <> d 4
1037-- s2 = 'streamly' $ d 3 <> d 4
1044−-- main = (S.'toList' . 'zipStreamly' $ (,) \<$> s1 \<*> s2) >>= print
1039+-- main = (S.'toList' . 'asZipStream' $ (,) \<$> s1 \<*> s2) >>= print
1047-- This takes total 10 seconds to zip, which is (1 + 2 + 3 + 4) since
1042-- This takes total 10 seconds to zip, which is (1 + 2 + 3 + 4) since
@@ -1058,7 +1053,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1058-- $parallelzip
1053-- $parallelzip
1060-- The applicative instance of 'ZipParallel' type zips streams concurrently.
1055-- The applicative instance of 'ZipParallel' type zips streams concurrently.
1061−-- 'zipParallely' type combinator can be used to switch to parallel applicative
1056+-- 'asZipParallel' type combinator can be used to switch to parallel applicative
1062-- zip composition:
1057-- zip composition:
@@ -1074,7 +1069,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1075-- main = do
1070-- main = do
1076-- hSetBuffering stdout LineBuffering
1071-- hSetBuffering stdout LineBuffering
1077−-- (S.'toList' . 'zipParallely' $ (,) \<$> s1 \<*> s2) >>= print
1072+-- (S.'toList' . 'asZipParallel' $ (,) \<$> s1 \<*> s2) >>= print
1080-- This takes 7 seconds to zip, which is max (1,3) + max (2,4) because 1 and 3
1075-- This takes 7 seconds to zip, which is max (1,3) + max (2,4) because 1 and 3
@@ -1098,11 +1093,11 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1098-- choose the desired type of concurrency.
1093-- choose the desired type of concurrency.
1100-- In the following example the squares of @x@ and @y@ are computed
1095-- In the following example the squares of @x@ and @y@ are computed
1101−-- concurrently using the 'coparallel' operation and the square roots of their
1096+-- concurrently using the 'parAhead' operation and the square roots of their
1102-- sum are computed serially because of the 'streamly' combinator. We can
1097-- sum are computed serially because of the 'streamly' combinator. We can
1103-- choose different combinators for the monadic processing and the stream
1098-- choose different combinators for the monadic processing and the stream
1104−-- generation, to control the concurrency. We can also use the 'coparallely'
1099+-- generation, to control the concurrency. We can also use the 'asParAhead'
1105−-- combinator instead of explicitly folding with 'coparallel'.
1100+-- combinator instead of explicitly folding with 'parAhead'.
1108-- import "Streamly"
1103-- import "Streamly"
@@ -1111,11 +1106,11 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1112-- main = do
1107-- main = do
1113-- z \<- S.'toList'
1108-- z \<- S.'toList'
1114−-- $ 'streamly' -- Serial monadic processing (sqrt below)
1109+-- $ 'asStream' -- Serial monadic processing (sqrt below)
1116−-- x2 \<- 'forEachWith' 'coparallel' [1..100] $ -- Concurrent @"for"@ loop
1111+-- x2 \<- 'forEachWith' 'parAhead' [1..100] $ -- Concurrent @"for"@ loop
1117-- \\x -> return $ x * x -- body of the loop
1112-- \\x -> return $ x * x -- body of the loop
1118−-- y2 \<- 'forEachWith' 'coparallel' [1..100] $
1113+-- y2 \<- 'forEachWith' 'parAhead' [1..100] $
1119-- \\y -> return $ y * y
1114-- \\y -> return $ y * y
1120-- return $ sqrt (x2 + y2)
1115-- return $ sqrt (x2 + y2)
1121-- print $ sum z
1116-- print $ sum z
@@ -1126,8 +1121,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1126-- and operators instead of the ugly pragmas.
1121-- and operators instead of the ugly pragmas.
1128-- For more concurrent programming examples see,
1123-- For more concurrent programming examples see,
1129−-- "Streamly.Examples.ListDirRecursive", "Streamly.Examples.MergeSortedStreams"
1124+-- "ListDir.hs", "MergeSort.hs" and "SearchQuery.hs" in the examples directory.
1130−-- and "Streamly.Examples.SearchEngineQuery".
1132-- $reactive
1126-- $reactive
@@ -1189,8 +1183,8 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
1189-- return ()
1183-- return ()
1192−-- You can also find the source of this example in
1186+-- You can also find the source of this example in the examples directory as
1193−-- "Streamly.Examples.AcidRainGame". It has been adapted from Gabriel's
1187+-- "AcidRain.hs". It has been adapted from Gabriel's
1194-- <https://hackage.haskell.org/package/pipes-concurrency-2.0.8/docs/Pipes-Concurrent-Tutorial.html pipes-concurrency>
1188-- <https://hackage.haskell.org/package/pipes-concurrency-2.0.8/docs/Pipes-Concurrent-Tutorial.html pipes-concurrency>
1195-- package.
1189-- package.
1196-- This is much simpler compared to the pipes version because of the builtin
1190-- This is much simpler compared to the pipes version because of the builtin