update concurrent programming example

This commit is contained in:
Harendra Kumar 2017-12-01 12:16:03 +05:30
parent 1d0ca9749e
commit cfafcff51c

View File

@ -103,7 +103,7 @@ module Streamly.Tutorial
-- * Summary of Compositions -- * Summary of Compositions
-- $compositionSummary -- $compositionSummary
-- * Concurrent Programming Examples -- * Concurrent Programming
-- $concurrent -- $concurrent
-- * Reactive Programming -- * Reactive Programming
@ -766,12 +766,17 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- $concurrent -- $concurrent
-- --
-- There are two ways to achieve concurrency. We can generate individual -- When writing concurrent programs there are two distinct places where the
-- elements of a stream concurrently by folding with parallel composition -- programmer chooses the type of concurrency. First, when /generating/ a
-- operators i.e. '<|' or '<|>'. 'forEachWith' can be useful in such cases. -- stream by combining other streams we can use one of the sum style operators
-- to combine them concurrently or serially. Second, when /processing/ a stream
-- in a monadic composition we can choose one of the monad composition types to
-- choose the desired type of concurrency.
-- --
-- In the following example, we square each number concurrently but then -- In the following example the squares of @x@ and @y@ are computed
-- sum and print them serially: -- concurrently using the '<|' operator and the square roots of their sum are
-- also computed concurrently by using the 'asyncly' combinator. We can choose
-- different combinators e.g. '<>' and 'serially', to control the concurrency.
-- --
-- @ -- @
-- import "Streamly" -- import "Streamly"
@ -779,26 +784,19 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- import Data.List (sum) -- import Data.List (sum)
-- --
-- main = do -- main = do
-- squares \<- 'toList' $ 'serially' $ 'forEachWith' ('<|') [1..100] $ \\x -\> return $ x * x -- z \<- 'toList'
-- print $ sum squares -- $ 'asyncly' -- Concurrent monadic processing (sqrt below)
-- @ -- $ do
-- -- x2 \<- 'forEachWith' ('<|') [1..100] $ -- Concurrent @for@ loop
-- The following example not just computes the squares concurrently but also -- \\x -> return $ x * x -- body of the loop
-- computes the square root of their sums concurrently by using the parallel -- y2 \<- 'forEachWith' ('<|') [1..100] $
-- monadic bind. -- \\y -> return $ y * y
-- -- return $ sqrt (x2 + y2)
-- @
-- import "Streamly"
-- import "Streamly.Prelude" (toList)
-- import Data.List (sum)
--
-- main = do
-- z \<- 'toList' $ 'asyncly' $ do
-- xsq \<- 'forEachWith' ('<|') [1..100] $ \\x -> return $ x * x
-- ysq \<- 'forEachWith' ('<|') [1..100] $ \\x -> return $ x * x
-- return $ sqrt (xsq + ysq)
-- print $ sum z -- print $ sum z
-- @ -- @
--
-- You can see how this directly maps to the imperative style
-- <https://en.wikipedia.org/wiki/OpenMP OpenMP> model.
-- $reactive -- $reactive
-- --