@@ -595,8 +595,16 @@ forkSVarAhead m1 m2 = mkStream $ \st yld sng stp -> do
596infixr 6 `ahead`
596infixr 6 `ahead`
598−-- | Polymorphic version of the 'Semigroup' operation '<>' of 'AheadT'.
598+-- | Appends two streams, some future actions may be evaluated concurrently,
599−-- Merges two streams sequentially but with concurrent lookahead.
599+-- ahead of time. The behavior is similar to 'consM' for 'AheadT' streams:
601+-- >>> stream = Stream.yieldM (delay 2) `ahead` Stream.yieldM (delay 1)
602+-- >>> Stream.toList stream -- IO [Int]
607+-- This operation can be used to fold an infinite lazy container of streams.
601-- /Since: 0.3.0 ("Streamly")/
609-- /Since: 0.3.0 ("Streamly")/
@@ -625,46 +633,43 @@ consMAhead m r = fromStream $ K.yieldM m `ahead` (toStream r)
626------------------------------------------------------------------------------
634------------------------------------------------------------------------------
628−-- | The 'Semigroup' operation for 'AheadT' appends two streams. The combined
636+-- | For 'AheadT' streams:
629−-- stream behaves like a single stream with the actions from the second stream
630−-- appended to the first stream. The combined stream is evaluated in the
631−-- speculative style. This operation can be used to fold an infinite lazy
632−-- container of streams.
635−-- import "Streamly.Prelude"
639+-- (<>) = 'Streamly.Prelude.ahead'
636−-- import qualified "Streamly.Prelude" as S
640+-- (>>=) = flip . 'Streamly.Prelude.concatMapWith' 'Streamly.Prelude.ahead'
637−-- import Control.Concurrent
640−-- xs \<- S.'toList' . S.'aheadly' $ (p 1 |: p 2 |: nil) <> (p 3 |: p 4 |: nil)
642−-- where p n = threadDelay 1000000 >> return n
648−-- Any exceptions generated by a constituent stream are propagated to the
643+-- Any exceptions generated by concurrent evaluation are propagated to the
649−-- output stream.
644+-- output stream as soon as they occur.
651−-- The monad instance of 'AheadT' may run each monadic continuation (bind)
646+-- A single 'Monad' bind behaves like a @for@ loop with iterations executed
652−-- concurrently in a speculative manner, performing side effects in a partially
647+-- concurrently, ahead of time, producing side effects of iterations out of
653−-- ordered manner but producing the outputs in an ordered manner like
648+-- order, but results in order:
657−-- main = S.drain . S.'aheadly' $ do
651+-- Stream.toList $ Stream.aheadly $ do
658−-- n <- return 3 \<\> return 2 \<\> return 1
652+-- x <- Stream.fromList [2,1] -- foreach x in stream
659−-- S.yieldM $ do
653+-- Stream.yieldM $ delay x
660−-- threadDelay (n * 1000000)
654+-- :}
661−-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
655+-- 1 sec
659+-- Nested monad binds behave like nested @for@ loops with nested iterations
660+-- executed concurrently, ahead of time:
664−-- ThreadId 40: Delay 1
663+-- Stream.toList $ Stream.aheadly $ do
665−-- ThreadId 39: Delay 2
664+-- x <- Stream.fromList [1,2] -- foreach x in stream
666−-- ThreadId 38: Delay 3
665+-- y <- Stream.fromList [2,4] -- foreach y in stream
667−-- @
666+-- Stream.yieldM $ delay (x + y)
669-- /Since: 0.3.0 ("Streamly")/
674-- /Since: 0.3.0 ("Streamly")/