Rename Streamly.Prelude.yieldM to fromEffect.

- Deprecate yieldM.
This commit is contained in:
Pranay Sashank 2021-05-29 02:05:01 +05:30
parent 4dfc6bf9ee
commit 70faf014ca
26 changed files with 139 additions and 126 deletions

View File

@ -93,6 +93,7 @@
* In `Streamly.Prelude` the following functions have been renamed:
* `concatUnfold` to `unfoldMany`
* `yield` to `fromPure`
* `yieldM` to `fromEffect`
* The following encoding/decoding routines have been renamed:
* `encodeUtf8Lax` to `encodeUtf8`
* `encodeLatin1Lax` to `encodeLatin1`

View File

@ -108,7 +108,7 @@ sourceFoldMapWith n = SP.foldMapWith S.serial S.fromPure [n..n+value]
{-# INLINE sourceFoldMapWithM #-}
sourceFoldMapWithM :: Monad m => Int -> Stream m Int
sourceFoldMapWithM n = SP.foldMapWith S.serial (S.yieldM . return) [n..n+value]
sourceFoldMapWithM n = SP.foldMapWith S.serial (S.fromEffect . return) [n..n+value]
-}
-------------------------------------------------------------------------------

View File

@ -159,7 +159,7 @@ concatMapFoldableSerial streamLen n = concatMapFoldableWith S.serial S.fromPure
{-# INLINE concatMapFoldableSerialM #-}
concatMapFoldableSerialM :: Monad m => Int -> Int -> Stream m Int
concatMapFoldableSerialM streamLen n =
concatMapFoldableWith S.serial (S.yieldM . return) [n..n+streamLen]
concatMapFoldableWith S.serial (S.fromEffect . return) [n..n+streamLen]
-------------------------------------------------------------------------------
-- Elimination
@ -446,7 +446,7 @@ inspect $ hasNoTypeClasses 'concatMapRepl
sourceConcatMapId :: Monad m
=> Int -> Int -> Stream m (Stream m Int)
sourceConcatMapId val n =
S.fromFoldable $ fmap (S.yieldM . return) [n..n+val]
S.fromFoldable $ fmap (S.fromEffect . return) [n..n+val]
{-# INLINE concatMapBySerial #-}
concatMapBySerial :: Int -> Int -> Int -> IO ()

View File

@ -51,7 +51,7 @@ readWriteOnExceptionStream inh devNull =
-- | Send the file contents to /dev/null with exception handling
readWriteHandleExceptionStream :: Handle -> Handle -> IO ()
readWriteHandleExceptionStream inh devNull =
let handler (_e :: SomeException) = S.yieldM (hClose inh >> return 10)
let handler (_e :: SomeException) = S.fromEffect (hClose inh >> return 10)
readEx = S.handle handler (S.unfold FH.read inh)
in S.fold (FH.write devNull) $ readEx

View File

@ -323,12 +323,12 @@ sourceFoldMapWithStream value n = S.concatMapFoldableWith (<>) S.fromPure
sourceFoldMapWithM :: (S.IsStream t, Monad m, Semigroup (t m Int))
=> Int -> Int -> t m Int
sourceFoldMapWithM value n =
S.concatMapFoldableWith (<>) (S.yieldM . return) [n..n+value]
S.concatMapFoldableWith (<>) (S.fromEffect . return) [n..n+value]
{-# INLINE sourceFoldMapM #-}
sourceFoldMapM :: (S.IsStream t, Monad m, Monoid (t m Int))
=> Int -> Int -> t m Int
sourceFoldMapM value n = F.foldMap (S.yieldM . return) [n..n+value]
sourceFoldMapM value n = F.foldMap (S.fromEffect . return) [n..n+value]
-------------------------------------------------------------------------------
-- Concat
@ -338,7 +338,7 @@ sourceFoldMapM value n = F.foldMap (S.yieldM . return) [n..n+value]
sourceConcatMapId :: (S.IsStream t, Monad m)
=> Int -> Int -> t m (t m Int)
sourceConcatMapId value n =
S.fromFoldable $ fmap (S.yieldM . return) [n..n+value]
S.fromFoldable $ fmap (S.fromEffect . return) [n..n+value]
-- concatMapWith

View File

@ -154,7 +154,7 @@ import qualified Streamly.Prelude as S
import Control.Concurrent (threadDelay)
main = S.toList $ S.fromParallel $ foldMap delay [1..10]
where delay n = S.yieldM $ threadDelay (n * 1000000) >> print n
where delay n = S.fromEffect $ threadDelay (n * 1000000) >> print n
```
Streams can be combined together in many ways. We provide some examples
@ -165,7 +165,7 @@ function in the examples to demonstrate the concurrency aspects:
import qualified Streamly.Prelude as S
import Control.Concurrent
delay n = S.yieldM $ do
delay n = S.fromEffect $ do
threadDelay (n * 1000000)
tid <- myThreadId
putStrLn (show tid ++ ": Delay " ++ show n)
@ -202,7 +202,7 @@ import qualified Streamly.Prelude as S
loops = do
x <- S.fromFoldable [1,2]
y <- S.fromFoldable [3,4]
S.yieldM $ putStrLn $ show (x, y)
S.fromEffect $ putStrLn $ show (x, y)
main = S.drain loops
```

View File

@ -30,7 +30,7 @@ monad.
You can write all of your program in a streamly monad and use the full power of
the library. Streamly can be used as a direct replacement of the IO monad with
no loss of performance, and no change in code except using `liftIO` or `yieldM`
no loss of performance, and no change in code except using `liftIO` or `fromEffect`
to run any IO actions. Streamly IO monads (e.g. `SerialT IO`) are just a
generalization of the IO monad with non-deterministic composition of streams
added on top.
@ -98,7 +98,7 @@ function:
```haskell
tuples <- S.toList $ fromZipAsync $
(,) <$> S.yieldM (getURLString 1) <*> S.yieldM (getURLText 2)
(,) <$> S.fromEffect (getURLString 1) <*> S.fromEffect (getURLText 2)
```
### race
@ -166,7 +166,7 @@ You can map a monadic action to a `Foldable` container to convert it into a
stream and at the same time fold it:
```haskell
urls <- S.toList $ fromAhead $ foldMap (S.yieldM . getURL) [1..3]
urls <- S.toList $ fromAhead $ foldMap (S.fromEffect . getURL) [1..3]
```
### replicateConcurrently

View File

@ -136,7 +136,7 @@ import Prelude hiding (map)
-- XXX It may be a good idea to increment sequence numbers for each yield,
-- currently a stream on the left side of the expression may yield many
-- elements with the same sequene number. We can then use the seq number to
-- enforce yieldMax and yieldLImit as well.
-- enforce fromEffectax and yieldLImit as well.
-- Invariants:
--
@ -620,8 +620,8 @@ infixr 6 `ahead`
-- are evaluated:
--
-- >>> import Streamly.Prelude (ahead)
-- >>> stream1 = Stream.yieldM (delay 4) :: SerialT IO Int
-- >>> stream2 = Stream.yieldM (delay 2) :: SerialT IO Int
-- >>> stream1 = Stream.fromEffect (delay 4) :: SerialT IO Int
-- >>> stream2 = Stream.fromEffect (delay 2) :: SerialT IO Int
-- >>> Stream.toList $ stream1 `ahead` stream2 :: IO [Int]
-- 2 sec
-- 4 sec
@ -630,7 +630,7 @@ infixr 6 `ahead`
-- Multiple streams can be combined. With enough threads, all of them can be
-- scheduled simultaneously:
--
-- >>> stream3 = Stream.yieldM (delay 1)
-- >>> stream3 = Stream.fromEffect (delay 1)
-- >>> Stream.toList $ stream1 `ahead` stream2 `ahead` stream3
-- 1 sec
-- 2 sec
@ -674,7 +674,7 @@ ahead m1 m2 = mkStream $ \st yld sng stp ->
{-# INLINE consMAhead #-}
{-# SPECIALIZE consMAhead :: IO a -> AheadT IO a -> AheadT IO a #-}
consMAhead :: MonadAsync m => m a -> AheadT m a -> AheadT m a
consMAhead m r = fromStream $ K.yieldM m `ahead` (toStream r)
consMAhead m r = fromStream $ K.fromEffect m `ahead` (toStream r)
------------------------------------------------------------------------------
-- AheadT
@ -694,7 +694,7 @@ consMAhead m r = fromStream $ K.yieldM m `ahead` (toStream r)
-- >>> :{
-- Stream.toList $ Stream.fromAhead $ do
-- x <- Stream.fromList [2,1] -- foreach x in stream
-- Stream.yieldM $ delay x
-- Stream.fromEffect $ delay x
-- :}
-- 1 sec
-- 2 sec
@ -707,7 +707,7 @@ consMAhead m r = fromStream $ K.yieldM m `ahead` (toStream r)
-- Stream.toList $ Stream.fromAhead $ do
-- x <- Stream.fromList [1,2] -- foreach x in stream
-- y <- Stream.fromList [2,4] -- foreach y in stream
-- Stream.yieldM $ delay (x + y)
-- Stream.fromEffect $ delay (x + y)
-- :}
-- 3 sec
-- 4 sec

View File

@ -657,8 +657,8 @@ infixr 6 `async`
-- outputs from both are used as they arrive:
--
-- >>> import Streamly.Prelude (async)
-- >>> stream1 = Stream.yieldM (delay 4)
-- >>> stream2 = Stream.yieldM (delay 2)
-- >>> stream1 = Stream.fromEffect (delay 4)
-- >>> stream2 = Stream.fromEffect (delay 2)
-- >>> Stream.toList $ stream1 `async` stream2
-- 2 sec
-- 4 sec
@ -667,7 +667,7 @@ infixr 6 `async`
-- Multiple streams can be combined. With enough threads, all of them can be
-- scheduled simultaneously:
--
-- >>> stream3 = Stream.yieldM (delay 1)
-- >>> stream3 = Stream.fromEffect (delay 1)
-- >>> Stream.toList $ stream1 `async` stream2 `async` stream3
-- ...
-- [1,2,4]
@ -735,7 +735,7 @@ async = joinStreamVarAsync AsyncVar
{-# INLINE consMAsync #-}
{-# SPECIALIZE consMAsync :: IO a -> AsyncT IO a -> AsyncT IO a #-}
consMAsync :: MonadAsync m => m a -> AsyncT m a -> AsyncT m a
consMAsync m r = fromStream $ K.yieldM m `async` (toStream r)
consMAsync m r = fromStream $ K.fromEffect m `async` (toStream r)
------------------------------------------------------------------------------
-- AsyncT
@ -755,7 +755,7 @@ consMAsync m r = fromStream $ K.yieldM m `async` (toStream r)
-- >>> :{
-- Stream.toList $ Stream.fromAsync $ do
-- x <- Stream.fromList [2,1] -- foreach x in stream
-- Stream.yieldM $ delay x
-- Stream.fromEffect $ delay x
-- :}
-- 1 sec
-- 2 sec
@ -768,7 +768,7 @@ consMAsync m r = fromStream $ K.yieldM m `async` (toStream r)
-- Stream.toList $ Stream.fromAsync $ do
-- x <- Stream.fromList [1,2] -- foreach x in stream
-- y <- Stream.fromList [2,4] -- foreach y in stream
-- Stream.yieldM $ delay (x + y)
-- Stream.fromEffect $ delay (x + y)
-- :}
-- 3 sec
-- 4 sec
@ -880,7 +880,7 @@ MONAD_COMMON_INSTANCES(AsyncT, MONADPARALLEL)
{-# INLINE consMWAsync #-}
{-# SPECIALIZE consMWAsync :: IO a -> WAsyncT IO a -> WAsyncT IO a #-}
consMWAsync :: MonadAsync m => m a -> WAsyncT m a -> WAsyncT m a
consMWAsync m r = fromStream $ K.yieldM m `wAsync` (toStream r)
consMWAsync m r = fromStream $ K.fromEffect m `wAsync` (toStream r)
infixr 6 `wAsync`
@ -968,7 +968,7 @@ wAsync = joinStreamVarAsync WAsyncVar
-- >>> :{
-- Stream.toList $ Stream.fromWAsync $ do
-- x <- Stream.fromList [2,1] -- foreach x in stream
-- Stream.yieldM $ delay x
-- Stream.fromEffect $ delay x
-- :}
-- 1 sec
-- 2 sec
@ -981,7 +981,7 @@ wAsync = joinStreamVarAsync WAsyncVar
-- Stream.toList $ Stream.fromWAsync $ do
-- x <- Stream.fromList [1,2] -- foreach x in stream
-- y <- Stream.fromList [2,4] -- foreach y in stream
-- Stream.yieldM $ delay (x + y)
-- Stream.fromEffect $ delay (x + y)
-- :}
-- 3 sec
-- 4 sec
@ -1071,7 +1071,7 @@ wAsync = joinStreamVarAsync WAsyncVar
-- @
-- main = S.'drain' . S.'fromWAsync' $ do
-- n <- return 3 \<\> return 2 \<\> return 1
-- S.yieldM $ do
-- S.fromEffect $ do
-- threadDelay (n * 1000000)
-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
-- @

View File

@ -15,7 +15,7 @@ module Streamly.Internal.Data.Stream.IsStream.Common
(
-- * Generation
fromPure
, yieldM
, fromEffect
, repeatM
, timesWith
, absTimesWith
@ -47,6 +47,7 @@ module Streamly.Internal.Data.Stream.IsStream.Common
-- * Deprecated
, yield
, yieldM
)
where
@ -91,7 +92,7 @@ import Prelude hiding (take, takeWhile, drop, reverse, concatMap)
-- Generation
------------------------------------------------------------------------------
-- Faster than yieldM because there is no bind.
-- Faster than fromEffect because there is no bind.
--
-- |
-- @
@ -104,7 +105,7 @@ import Prelude hiding (take, takeWhile, drop, reverse, concatMap)
--
-- @
-- fromPure = pure
-- fromPure = yieldM . pure
-- fromPure = fromEffect . pure
-- @
--
-- In Zip applicative streams 'fromPure' is not the same as 'pure' because in that
@ -120,26 +121,27 @@ fromPure = K.fromPure
-- |
-- @
-- yieldM m = m \`consM` nil
-- fromEffect m = m \`consM` nil
-- @
--
-- Create a singleton stream from a monadic action.
--
-- @
-- > Stream.toList $ Stream.yieldM getLine
-- > Stream.toList $ Stream.fromEffect getLine
-- hello
-- ["hello"]
-- @
--
-- @since 0.4.0
{-# INLINE yieldM #-}
yieldM :: (Monad m, IsStream t) => m a -> t m a
yieldM = K.yieldM
-- /Since: 0.8.0 (Renamed yieldM to fromEffect)/
--
{-# INLINE fromEffect #-}
fromEffect :: (Monad m, IsStream t) => m a -> t m a
fromEffect = K.fromEffect
-- |
-- @
-- repeatM = fix . consM
-- repeatM = cycle1 . yieldM
-- repeatM = cycle1 . fromEffect
-- @
--
-- Generate a stream by repeatedly executing a monadic action forever.
@ -477,7 +479,7 @@ concatMap f m = fromStreamD $ D.concatMap (toStreamD . f) (toStreamD m)
-- monad with the stream monad.
--
-- @
-- concatM = concat . yieldM
-- concatM = concat . fromEffect
-- concatM = concat . lift -- requires @(MonadTrans t)@
-- concatM = join . lift -- requires @(MonadTrans t@, @Monad (t m))@
-- @
@ -550,10 +552,18 @@ splitOnSeq
=> Array a -> Fold m a b -> t m a -> t m b
splitOnSeq patt f m = D.fromStreamD $ D.splitOnSeq patt f (D.toStreamD m)
-- | Same as 'yield'
-- | Same as 'fromPure'
--
-- @since 0.4.0
{-# DEPRECATED yield "Please use fromPure instead." #-}
{-# INLINE yield #-}
yield :: IsStream t => a -> t m a
yield = fromPure
-- | Same as 'fromEffect'
--
-- @since 0.4.0
{-# DEPRECATED yieldM "Please use fromEffect instead." #-}
{-# INLINE yieldM #-}
yieldM :: (Monad m, IsStream t) => m a -> t m a
yieldM = fromEffect

View File

@ -161,7 +161,7 @@ where
import Streamly.Internal.Data.Stream.Ahead (ahead)
import Streamly.Internal.Data.Stream.Async (async, wAsync)
import Streamly.Internal.Data.Stream.IsStream.Common
(concatM, concatMapM, concatMap, smapM, fromPure, yieldM)
(concatM, concatMapM, concatMap, smapM, fromPure, fromEffect)
import Streamly.Internal.Data.Stream.Parallel (parallel)
import Streamly.Internal.Data.Stream.Prelude
( concatFoldableWith, concatMapFoldableWith
@ -714,7 +714,7 @@ concatPairsWith = K.concatPairsWith
-- Note that 'iterateM' is a special case of 'iterateMapWith':
--
-- @
-- iterateM f = iterateMapWith serial (yieldM . f) . yieldM
-- iterateM f = iterateMapWith serial (fromEffect . f) . fromEffect
-- @
--
-- It can be used to traverse a tree structure. For example, to list a
@ -774,7 +774,7 @@ iterateSmapMWith
-> t m a
-> t m a
iterateSmapMWith combine f initial stream =
concatMap (\b -> concatMapWith combine (go b) stream) (yieldM initial)
concatMap (\b -> concatMapWith combine (go b) stream) (fromEffect initial)
where
@ -783,7 +783,7 @@ iterateSmapMWith combine f initial stream =
feedback b a =
concatMap
(\(b1, s) -> concatMapWith combine (go b1) s)
(yieldM $ f b a)
(fromEffect $ f b a)
------------------------------------------------------------------------------
-- iterateMap - Either streams

View File

@ -36,7 +36,7 @@ module Streamly.Internal.Data.Stream.IsStream.Generate
-- * From Values
, fromPure
, yieldM
, fromEffect
, repeat
, repeatM
, replicate
@ -80,6 +80,7 @@ module Streamly.Internal.Data.Stream.IsStream.Generate
-- * Deprecated
, K.once
, yield
, yieldM
, each
, fromHandle
, currentTime
@ -95,8 +96,8 @@ import Streamly.Internal.Data.SVar (MonadAsync, Rate (..))
import Streamly.Internal.Data.Stream.IsStream.Enumeration
(Enumerable(..), enumerate, enumerateTo)
import Streamly.Internal.Data.Stream.IsStream.Common
( absTimesWith, concatM, relTimesWith, timesWith, fromPure, yield, yieldM
, repeatM)
( absTimesWith, concatM, relTimesWith, timesWith, fromPure, fromEffect
, yield, yieldM, repeatM)
import Streamly.Internal.Data.Stream.Prelude (fromStreamS)
import Streamly.Internal.Data.Stream.StreamD (fromStreamD)
import Streamly.Internal.Data.Stream.StreamK (IsStream((|:), consM))

View File

@ -231,7 +231,7 @@ joinStreamVarPar style ss m1 m2 = mkStream $ \st yld sng stp ->
{-# INLINE consMParallel #-}
{-# SPECIALIZE consMParallel :: IO a -> ParallelT IO a -> ParallelT IO a #-}
consMParallel :: MonadAsync m => m a -> ParallelT m a -> ParallelT m a
consMParallel m r = fromStream $ K.yieldM m `parallel` (toStream r)
consMParallel m r = fromStream $ K.fromEffect m `parallel` (toStream r)
infixr 6 `parallel`
@ -243,7 +243,7 @@ infixr 6 `parallel`
-- Evaluation may block if the output buffer becomes full.
--
-- >>> import Streamly.Prelude (parallel)
-- >>> stream = Stream.yieldM (delay 2) `parallel` Stream.yieldM (delay 1)
-- >>> stream = Stream.fromEffect (delay 2) `parallel` Stream.fromEffect (delay 1)
-- >>> Stream.toList stream -- IO [Int]
-- 1 sec
-- 2 sec

View File

@ -39,7 +39,7 @@ module Streamly.Internal.Data.Stream.StreamD.Generate
-- * From Values
, fromPure
, yieldM
, fromEffect
, repeat
, repeatM
, replicate

View File

@ -31,7 +31,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type
-- * From Values
, fromPure
, yieldM
, fromEffect
-- * From Containers
, fromList
@ -197,9 +197,9 @@ fromPure x = Stream (\_ s -> pure $ step undefined s) True
step _ False = Stop
-- | Create a singleton 'Stream' from a monadic action.
{-# INLINE_NORMAL yieldM #-}
yieldM :: Monad m => m a -> Stream m a
yieldM m = Stream step True
{-# INLINE_NORMAL fromEffect #-}
fromEffect :: Monad m => m a -> Stream m a
fromEffect m = Stream step True
where
{-# INLINE_LATE step #-}
step _ True = m >>= \x -> return $ Yield x False
@ -373,7 +373,7 @@ foldrS f final (Stream step state) = go SPEC state
{-# INLINE_LATE go #-}
go !_ st = do
-- defState??
r <- yieldM $ step defState st
r <- fromEffect $ step defState st
case r of
Yield x s -> f x (go SPEC s)
Skip s -> go SPEC s
@ -965,7 +965,7 @@ groupsOf2 n input (Fold2 fstep inject extract) (Stream step state) =
instance MonadTrans Stream where
{-# INLINE lift #-}
lift = yieldM
lift = fromEffect
instance (MonadThrow m) => MonadThrow (Stream m) where
throwM = lift . throwM

View File

@ -62,7 +62,7 @@ module Streamly.Internal.Data.Stream.StreamK
-- ** Conversions
, fromPure
, yieldM
, fromEffect
, fromFoldable
, fromList
, fromStreamK
@ -261,13 +261,13 @@ unfoldrA = undefined
-- Special generation
-------------------------------------------------------------------------------
-- | Same as yieldM
-- | Same as fromEffect
--
-- @since 0.2.0
{-# DEPRECATED once "Please use yieldM instead." #-}
{-# DEPRECATED once "Please use fromEffect instead." #-}
{-# INLINE once #-}
once :: (Monad m, IsStream t) => m a -> t m a
once = yieldM
once = fromEffect
-- |
-- @

View File

@ -54,7 +54,7 @@ module Streamly.Internal.Data.Stream.StreamK.Type
, (.:)
, consMStream
, consMBy
, yieldM
, fromEffect
, fromPure
, nil
@ -349,15 +349,15 @@ nilM m = mkStream $ \_ _ _ stp -> m >> stp
fromPure :: IsStream t => a -> t m a
fromPure a = mkStream $ \_ _ single _ -> single a
{-# INLINE_NORMAL yieldM #-}
yieldM :: (Monad m, IsStream t) => m a -> t m a
yieldM m = fromStream $ mkStream $ \_ _ single _ -> m >>= single
{-# INLINE_NORMAL fromEffect #-}
fromEffect :: (Monad m, IsStream t) => m a -> t m a
fromEffect m = fromStream $ mkStream $ \_ _ single _ -> m >>= single
-- XXX specialize to IO?
{-# INLINE consMBy #-}
consMBy :: (IsStream t, MonadAsync m) => (t m a -> t m a -> t m a)
-> m a -> t m a -> t m a
consMBy f m r = (fromStream $ yieldM m) `f` r
consMBy f m r = (fromStream $ fromEffect m) `f` r
------------------------------------------------------------------------------
-- Folding a stream
@ -534,7 +534,7 @@ foldrSM = foldrSMWith foldStream
-- {-# RULES "foldrSM/id" foldrSM consM nil = \x -> x #-}
{-# RULES "foldrSM/nil" forall k z. foldrSM k z nil = z #-}
{-# RULES "foldrSM/single" forall k z x. foldrSM k z (yieldM x) = k x z #-}
{-# RULES "foldrSM/single" forall k z x. foldrSM k z (fromEffect x) = k x z #-}
-- {-# RULES "foldrSM/app" [1]
-- forall ys. foldrSM consM ys = \xs -> xs `conjoin` ys #-}
@ -548,7 +548,7 @@ foldrSMShared = foldrSMWith foldStreamShared
{-# RULES "foldrSMShared/nil"
forall k z. foldrSMShared k z nil = z #-}
{-# RULES "foldrSMShared/single"
forall k z x. foldrSMShared k z (yieldM x) = k x z #-}
forall k z x. foldrSMShared k z (fromEffect x) = k x z #-}
-- {-# RULES "foldrSM/app" [1]
-- forall ys. foldrSM consM ys = \xs -> xs `conjoin` ys #-}
@ -933,7 +933,7 @@ instance Monad m => Functor (Stream m) where
instance MonadTrans Stream where
{-# INLINE lift #-}
lift = yieldM
lift = fromEffect
-------------------------------------------------------------------------------
-- Nesting

View File

@ -340,7 +340,7 @@ putChunks
-> SerialT m (Array Word8)
-> m ()
putChunks addr port xs =
S.drain $ withConnection addr port (\sk -> S.yieldM $ ISK.putChunks sk xs)
S.drain $ withConnection addr port (\sk -> S.fromEffect $ ISK.putChunks sk xs)
-- | Write a stream of arrays to the supplied IPv4 host address and port
-- number.

View File

@ -348,7 +348,7 @@ module Streamly.Prelude
-- ** From Values
-- | Generate a monadic stream from a seed value or values.
, fromPure
, yieldM
, fromEffect
, repeat
, repeatM
, replicate
@ -921,6 +921,7 @@ module Streamly.Prelude
-- * Deprecated
, once
, yield
, yieldM
, each
, scanx
, foldx

View File

@ -192,7 +192,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- represents a single IO action whereas the 'Serial' monad represents a series
-- of IO actions. The only change you need to make to go from 'IO' to 'Serial'
-- is to use 'drain' to run the monad and to prefix the IO actions with
-- either 'yieldM' or 'liftIO'. If you use liftIO you can switch from 'Serial'
-- either 'fromEffect' or 'liftIO'. If you use liftIO you can switch from 'Serial'
-- to IO monad by simply removing the 'drain' function; no other changes
-- are needed unless you have used some stream specific composition or
-- combinators.
@ -364,7 +364,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- @
--
-- To create a singleton stream from a pure value use 'fromPure' or 'pure' and to
-- create a singleton stream from a monadic action use 'yieldM'. Note that in
-- create a singleton stream from a monadic action use 'fromEffect'. Note that in
-- case of Zip applicative streams "pure" repeats the value to generate an
-- infinite stream.
--
@ -373,7 +373,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- [1]
-- > S.'toList' $ S.'fromPure' 1
-- [1]
-- > S.'toList' $ S.'yieldM' 'getLine'
-- > S.'toList' $ S.'fromEffect' 'getLine'
-- hello
-- ["hello"]
-- @
@ -529,7 +529,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- seconds. After the delay it prints the number of seconds it slept.
--
-- @
-- delay n = S.'yieldM' $ do
-- delay n = S.'fromEffect' $ do
-- threadDelay (n * 1000000)
-- tid \<- myThreadId
-- putStrLn (show tid ++ ": Delay " ++ show n)
@ -760,7 +760,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
--
-- @
-- main = S.'drain' . S.'fromAsync' $ traced (sqrt 9) '<>' traced (sqrt 16) '<>' traced (sqrt 25)
-- where traced m = S.'yieldM' (myThreadId >>= print) >> return m
-- where traced m = S.'fromEffect' (myThreadId >>= print) >> return m
-- @
-- @
-- ThreadId 40
@ -882,7 +882,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- google = get "https://www.google.com/search?q=haskell"
-- bing = get "https://www.bing.com/search?q=haskell"
-- duckduckgo = get "https://www.duckduckgo.com/?q=haskell"
-- get s = S.'yieldM' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
-- get s = S.'fromEffect' (httpNoBody (parseRequest_ s) >> putStrLn (show s))
-- @
--
-- The polymorphic version of the binary operation '<>' of the 'Parallel' type
@ -941,7 +941,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- S.'drain' $ S.'concatFoldableWith' S.'async' (map delay [1..10])
-- S.'drain' $ S.'concatMapFoldableWith' S.'async' delay [1..10]
-- S.'drain' $ S.'concatForFoldableWith' S.'async' [1..10] delay
-- where delay n = S.'yieldM' $ threadDelay (n * 1000000) >> print n
-- where delay n = S.'fromEffect' $ threadDelay (n * 1000000) >> print n
-- @
-- $nesting
@ -1011,7 +1011,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
--
-- import Control.Monad (forever)
--
-- main = S.'drain' $ forever $ S.'yieldM' getLine >>= S.'yieldM' . putStrLn
-- main = S.'drain' $ forever $ S.'fromEffect' getLine >>= S.'fromEffect' . putStrLn
-- @
--
-- When multiple streams are composed using this style they nest in a DFS
@ -1025,7 +1025,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- main = S.'drain' $ do
-- x <- S.'fromFoldable' [1,2]
-- y <- S.'fromFoldable' [3,4]
-- S.'yieldM' $ putStrLn $ show (x, y)
-- S.'fromEffect' $ putStrLn $ show (x, y)
-- @
-- @
-- (1,3)
@ -1119,7 +1119,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- main = S.'drain' . S.'fromAsync' $ do
-- x <- S.'fromFoldable' [1,2]
-- y <- S.'fromFoldable' [3,4]
-- S.'yieldM' $ putStrLn $ show (x, y)
-- S.'fromEffect' $ putStrLn $ show (x, y)
-- @
-- @
-- (1,3)
@ -1143,7 +1143,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- main = S.'drain' . S.'fromWSerial' $ do
-- x <- S.'fromFoldable' [1,2]
-- y <- S.'fromFoldable' [3,4]
-- S.yieldM $ putStrLn $ show (x, y)
-- S.fromEffect $ putStrLn $ show (x, y)
-- @
-- @
-- (1,3)
@ -1170,7 +1170,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- main = S.'drain' . S.'fromWAsync' $ do
-- x <- S.'fromFoldable' [1,2]
-- y <- S.'fromFoldable' [3,4]
-- S.'yieldM' $ putStrLn $ show (x, y)
-- S.'fromEffect' $ putStrLn $ show (x, y)
-- @
-- @
-- (1,3)
@ -1217,7 +1217,7 @@ import Control.Monad.Trans.Class (MonadTrans (lift))
-- sz <- sizes
-- cl <- colors
-- sh <- shapes
-- S.'yieldM' $ putStrLn $ show (sz, cl, sh)
-- S.'fromEffect' $ putStrLn $ show (sz, cl, sh)
--
-- where
--

View File

@ -154,9 +154,9 @@ checkEvents rootPath m matchList = do
checker :: S.IsStream t =>
FilePath -> MVar () -> [String] -> t IO String
checker rootPath synch matchList =
S.yieldM (checkEvents rootPath synch matchList)
S.fromEffect (checkEvents rootPath synch matchList)
`S.parallelFst`
S.yieldM timeout
S.fromEffect timeout
-------------------------------------------------------------------------------
-- Test Drivers
@ -178,7 +178,7 @@ driver (desc, pre, ops, events) = it desc $ runTest `shouldReturn` "PASS"
withSystemTempDirectory fseventDir $ \fp -> do
pre fp
let eventStream = checker fp sync events
fsOps = S.yieldM $ runFSOps fp sync
fsOps = S.fromEffect $ runFSOps fp sync
fmap fromJust $ S.head $ eventStream `S.parallelFst` fsOps
runFSOps fp sync = do

View File

@ -77,21 +77,21 @@ mixedOps =
composeMixed :: SerialT IO Int
composeMixed = do
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
let x = 1
let y = 2
z <- do
x1 <- S.fromWAsync $ return 1 <> return 2
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
y1 <- S.fromAsync $ return 1 <> return 2
z1 <- do
x11 <- return 1 <> return 2
y11 <- S.fromAsync $ return 1 <> return 2
z11 <- S.fromWSerial $ return 1 <> return 2
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
return (x11 + y11 + z11)
return (x1 + y1 + z1)
return (x + y + z)
@ -107,21 +107,21 @@ mixedOpsAheadly =
composeMixed :: SerialT IO Int
composeMixed = do
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
let x = 1
let y = 2
z <- do
x1 <- S.fromWAsync $ return 1 <> return 2
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
y1 <- S.fromAhead $ return 1 <> return 2
z1 <- do
x11 <- return 1 <> return 2
y11 <- S.fromAhead $ return 1 <> return 2
z11 <- S.fromParallel $ return 1 <> return 2
S.yieldM $ return ()
S.yieldM $ putStr ""
S.fromEffect $ return ()
S.fromEffect $ putStr ""
return (x11 + y11 + z11)
return (x1 + y1 + z1)
return (x + y + z)
@ -129,20 +129,20 @@ mixedOpsAheadly =
-- XXX Merge both the loops.
nestedLoops :: IO ()
nestedLoops = S.drain $ do
S.yieldM $ hSetBuffering stdout LineBuffering
S.fromEffect $ hSetBuffering stdout LineBuffering
x <- loop "A " 2
y <- loop "B " 2
S.yieldM $ myThreadId >>= putStr . show
S.fromEffect $ myThreadId >>= putStr . show
>> putStr " "
>> print (x, y)
where
-- we can just use
-- fromParallel $ mconcat $ replicate n $ yieldM (...)
-- fromParallel $ mconcat $ replicate n $ fromEffect (...)
loop :: String -> Int -> SerialT IO String
loop name n = do
rnd <- S.yieldM (randomIO :: IO Int)
rnd <- S.fromEffect (randomIO :: IO Int)
let result = name <> show rnd
repeatIt = if n > 1 then loop name (n - 1) else S.nil
in return result `S.wAsync` repeatIt
@ -152,19 +152,19 @@ parallelLoops = do
hSetBuffering stdout LineBuffering
S.drain $ do
x <- S.take 10 $ loop "A" `S.parallel` loop "B"
S.yieldM $ myThreadId >>= putStr . show
S.fromEffect $ myThreadId >>= putStr . show
>> putStr " got "
>> print x
where
-- we can just use
-- fromParallel $ cycle1 $ yieldM (...)
-- fromParallel $ cycle1 $ fromEffect (...)
loop :: String -> SerialT IO (String, Int)
loop name = do
S.yieldM $ threadDelay 1000000
rnd <- S.yieldM (randomIO :: IO Int)
S.yieldM $ myThreadId >>= putStr . show
S.fromEffect $ threadDelay 1000000
rnd <- S.fromEffect (randomIO :: IO Int)
S.fromEffect $ myThreadId >>= putStr . show
>> putStr " yielding "
>> print rnd
return (name, rnd) `S.parallel` loop name

View File

@ -129,10 +129,10 @@ concurrentUnfoldrM eq op n =
-- results may not be yielded in order, in case of
-- Async/WAsync/Parallel. So we use an increasing count
-- instead.
i <- S.yieldM $ readIORef cnt
S.yieldM $ modifyIORef cnt (+1)
i <- S.fromEffect $ readIORef cnt
S.fromEffect $ modifyIORef cnt (+1)
let msg = show i <> "/" <> show n
S.yieldM $
S.fromEffect $
when (even i) $ do
dbgMVar ("first take concurrentUnfoldrM " <> msg)
(takeMVar mv)
@ -484,12 +484,12 @@ main = hspec
it "asyncly crosses thread limit (2000 threads)" $
S.drain (fromAsync $ fold $
replicate 2000 $ S.yieldM $ threadDelay 1000000)
replicate 2000 $ S.fromEffect $ threadDelay 1000000)
`shouldReturn` ()
it "aheadly crosses thread limit (4000 threads)" $
S.drain (fromAhead $ fold $
replicate 4000 $ S.yieldM $ threadDelay 1000000)
replicate 4000 $ S.fromEffect $ threadDelay 1000000)
`shouldReturn` ()
#ifdef DEVBUILD

View File

@ -473,7 +473,7 @@ testFromCallback = do
setCallback ref cb = do
writeIORef ref (Just cb)
runCallback ref = S.yieldM $ do
runCallback ref = S.fromEffect $ do
cb <-
S.repeatM (readIORef ref)
& IS.delayPost 0.1
@ -526,7 +526,7 @@ main = hspec
it "simple serially" $
(S.drain . fromSerial) (return (0 :: Int)) `shouldReturn` ()
it "simple serially with IO" $
(S.drain . fromSerial) (S.yieldM $ putStrLn "hello") `shouldReturn` ()
(S.drain . fromSerial) (S.fromEffect $ putStrLn "hello") `shouldReturn` ()
describe "Empty" $
it "Monoid - mempty" $
@ -559,7 +559,7 @@ main = hspec
serialOps $ prop "serially fromListM" . constructWithFromListM id
serialOps $ prop "serially unfoldr" . constructWithUnfoldr id
serialOps $ prop "serially fromPure" . constructWithYield id
serialOps $ prop "serially yieldM" . constructWithYieldM id
serialOps $ prop "serially fromEffect" . constructWithFromEffect id
serialOps $ prop "serially cons" . constructWithCons S.cons
serialOps $ prop "serially consM" . constructWithConsM S.consM id
serialOps $ prop "serially (.:)" . constructWithCons (S..:)

View File

@ -122,7 +122,7 @@ main = hspec
#ifdef DEVBUILD
let timed :: (IsStream t, Monad (t IO)) => Int -> t IO Int
timed x = S.yieldM (threadDelay (x * 100000)) >> return x
timed x = S.fromEffect (threadDelay (x * 100000)) >> return x
-- These are not run fromParallel because the timing gets affected
-- unpredictably when other tests are running on the same machine.

View File

@ -30,7 +30,7 @@ module Streamly.Test.Prelude.Common
, constructWithCons
, constructWithConsM
, constructWithYield
, constructWithYieldM
, constructWithFromEffect
, simpleOps
-- * Applicative operations
, applicativeOps
@ -415,7 +415,7 @@ constructWithYield listT op len =
let list = replicate (fromIntegral len) 0
listEquals (==) (listT strm) list
constructWithYieldM ::
constructWithFromEffect ::
(IsStream t
#if __GLASGOW_HASKELL__ < 806
, Monoid (t IO Int)
@ -425,13 +425,13 @@ constructWithYieldM ::
-> (t IO Int -> SerialT IO Int)
-> Word8
-> Property
constructWithYieldM listT op len =
constructWithFromEffect listT op len =
withMaxSuccess maxTestCount $
monadicIO $ do
strm <-
run
$ S.toList . op . S.take (fromIntegral len)
$ foldMap S.yieldM (repeat (return 0))
$ foldMap S.fromEffect (repeat (return 0))
let list = replicate (fromIntegral len) 0
listEquals (==) (listT strm) list
@ -447,7 +447,7 @@ simpleProps constr op a = monadicIO $ do
simpleOps :: IsStream t => (t IO Int -> SerialT IO Int) -> Spec
simpleOps op = do
prop "fromPure a = a" $ simpleProps S.fromPure op
prop "yieldM a = a" $ simpleProps (S.yieldM . return) op
prop "fromEffect a = a" $ simpleProps (S.fromEffect . return) op
-------------------------------------------------------------------------------
-- Applicative operations
@ -786,12 +786,12 @@ loops t tsrt hsrt = do
where
loopHead x = do
-- this print line is important for the test (causes a bind)
S.yieldM $ putStrLn "LoopHead..."
S.fromEffect $ putStrLn "LoopHead..."
t $ (if x < 3 then loopHead (x + 1) else nil) <> return x
loopTail x = do
-- this print line is important for the test (causes a bind)
S.yieldM $ putStrLn "LoopTail..."
S.fromEffect $ putStrLn "LoopTail..."
t $ return x <> (if x < 3 then loopTail (x + 1) else nil)
---------------------------------------------------------------------------
@ -1385,7 +1385,7 @@ parallelCheck t f = do
(S.toList . t) (event 4 `f` (event 3 `f` (event 2 `f` event 1)))
`shouldReturn` [1..4]
where event n = S.yieldM (threadDelay (n * 200000)) >> return n
where event n = S.fromEffect (threadDelay (n * 200000)) >> return n
-------------------------------------------------------------------------------
-- Exception ops