Skip to content

Commit

Permalink
Deprecate Stream.yieldM and rename it to Stream.valueM.
Browse files Browse the repository at this point in the history
- Update Changelog.
  • Loading branch information
pranaysashank committed Mar 10, 2021
1 parent 450d6fa commit a0ebf77
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 114 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Expand Up @@ -66,6 +66,7 @@
* Drop support for GHC 7.10.3.
* The following functions in 'Streamly.Prelude' have been renamed:
* `yield` to `value`
* `yieldM` to `valueM`
* The following functions in `Streamly.Data.Fold` have been renamed:
* `mapM` to `rmapM`
* `sequence` to `rsequence`
Expand Down
2 changes: 1 addition & 1 deletion benchmark/Streamly/Benchmark/Data/Stream/StreamDK.hs
Expand Up @@ -108,7 +108,7 @@ sourceFoldMapWith n = SP.foldMapWith S.serial S.value [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.valueM . return) [n..n+value]
-}

-------------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs
Expand Up @@ -130,7 +130,7 @@ sourceFoldMapWith n = concatMapFoldableWith S.serial S.value [n..n+value]
{-# INLINE sourceFoldMapWithM #-}
sourceFoldMapWithM :: Monad m => Int -> Stream m Int
sourceFoldMapWithM n =
concatMapFoldableWith S.serial (S.yieldM . return) [n..n+value]
concatMapFoldableWith S.serial (S.valueM . return) [n..n+value]

-------------------------------------------------------------------------------
-- Elimination
Expand Down Expand Up @@ -362,7 +362,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.valueM . return) [n..n+val]

{-# INLINE concatStreamsWith #-}
concatStreamsWith :: Int -> Int -> Int -> IO ()
Expand Down
2 changes: 1 addition & 1 deletion benchmark/Streamly/Benchmark/Prelude/Serial/Exceptions.hs
Expand Up @@ -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.valueM (hClose inh >> return 10)
readEx = S.handle handler (S.unfold FH.read inh)
in S.fold (FH.write devNull) $ readEx

Expand Down
6 changes: 3 additions & 3 deletions benchmark/lib/Streamly/Benchmark/Prelude.hs
Expand Up @@ -323,12 +323,12 @@ sourceFoldMapWithStream value n = S.concatMapFoldableWith (<>) S.value
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.valueM . 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.valueM . return) [n..n+value]

-------------------------------------------------------------------------------
-- Concat
Expand All @@ -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.valueM . return) [n..n+value]

-- concatMapWith

Expand Down
2 changes: 1 addition & 1 deletion src/Streamly/Internal/Data/Fold/Types.hs
Expand Up @@ -563,7 +563,7 @@ instance Functor m => Functor (Fold m a) where
yield :: Applicative m => b -> Fold m a b
yield b = Fold undefined (pure $ Done b) pure

-- This is the dual of stream "yieldM".
-- This is the dual of stream "valueM".
--
-- | A fold that always yields the result of an effectful action without
-- consuming any input.
Expand Down
4 changes: 2 additions & 2 deletions src/Streamly/Internal/Data/Stream/Ahead.hs
Expand Up @@ -619,7 +619,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.valueM m `ahead` (toStream r)

------------------------------------------------------------------------------
-- AheadT
Expand Down Expand Up @@ -656,7 +656,7 @@ consMAhead m r = fromStream $ K.yieldM m `ahead` (toStream r)
-- @
-- main = S.drain . S.'aheadly' $ do
-- n <- return 3 \<\> return 2 \<\> return 1
-- S.yieldM $ do
-- S.valueM $ do
-- threadDelay (n * 1000000)
-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
-- @
Expand Down
8 changes: 4 additions & 4 deletions src/Streamly/Internal/Data/Stream/Async.hs
Expand Up @@ -663,7 +663,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.valueM m `async` (toStream r)

------------------------------------------------------------------------------
-- AsyncT
Expand Down Expand Up @@ -705,7 +705,7 @@ consMAsync m r = fromStream $ K.yieldM m `async` (toStream r)
-- @
-- main = S.'drain' . S.'asyncly' $ do
-- n <- return 3 \<\> return 2 \<\> return 1
-- S.yieldM $ do
-- S.valueM $ do
-- threadDelay (n * 1000000)
-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
-- @
Expand Down Expand Up @@ -814,7 +814,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.valueM m `wAsync` (toStream r)

infixr 6 `wAsync`

Expand Down Expand Up @@ -913,7 +913,7 @@ wAsync = joinStreamVarAsync WAsyncVar
-- @
-- main = S.'drain' . S.'wAsyncly' $ do
-- n <- return 3 \<\> return 2 \<\> return 1
-- S.yieldM $ do
-- S.valueM $ do
-- threadDelay (n * 1000000)
-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
-- @
Expand Down
22 changes: 11 additions & 11 deletions src/Streamly/Internal/Data/Stream/IsStream/Common.hs
Expand Up @@ -15,7 +15,7 @@ module Streamly.Internal.Data.Stream.IsStream.Common
(
-- * Generation
value
, yieldM
, valueM
, repeatM
, timesWith
, absTimesWith
Expand Down Expand Up @@ -87,7 +87,7 @@ import Prelude hiding (take, takeWhile, drop, reverse)
-- Generation
------------------------------------------------------------------------------

-- Faster than yieldM because there is no bind.
-- Faster than valueM because there is no bind.
--
-- |
-- @
Expand All @@ -100,7 +100,7 @@ import Prelude hiding (take, takeWhile, drop, reverse)
--
-- @
-- value = pure
-- value = yieldM . pure
-- value = valueM . pure
-- @
--
-- In Zip applicative streams 'value' is not the same as 'pure' because in that
Expand All @@ -115,26 +115,26 @@ value = K.value

-- |
-- @
-- yieldM m = m \`consM` nil
-- valueM m = m \`consM` nil
-- @
--
-- Create a singleton stream from a monadic action.
--
-- @
-- > Stream.toList $ Stream.yieldM getLine
-- > Stream.toList $ Stream.valueM 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
{-# INLINE valueM #-}
valueM :: (Monad m, IsStream t) => m a -> t m a
valueM = K.valueM

-- |
-- @
-- repeatM = fix . consM
-- repeatM = cycle1 . yieldM
-- repeatM = cycle1 . valueM
-- @
--
-- Generate a stream by repeatedly executing a monadic action forever.
Expand Down Expand Up @@ -448,7 +448,7 @@ concatMapM f m = fromStreamD $ D.concatMapM (fmap toStreamD . f) (toStreamD m)
-- monad with the stream monad.
--
-- @
-- concatM = concat . yieldM
-- concatM = concat . valueM
-- concatM = concat . lift -- requires @(MonadTrans t)@
-- concatM = join . lift -- requires @(MonadTrans t@, @Monad (t m))@
-- @
Expand Down
13 changes: 11 additions & 2 deletions src/Streamly/Internal/Data/Stream/IsStream/Generate.hs
Expand Up @@ -36,7 +36,7 @@ module Streamly.Internal.Data.Stream.IsStream.Generate

-- * From Values
, value
, yieldM
, valueM
, repeat
, repeatM
, replicate
Expand Down Expand Up @@ -80,6 +80,7 @@ module Streamly.Internal.Data.Stream.IsStream.Generate
-- * Deprecated
, K.once
, yield
, yieldM
, each
, fromHandle
, currentTime
Expand All @@ -95,7 +96,7 @@ 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, value, yieldM, repeatM)
(absTimesWith, concatM, relTimesWith, timesWith, value, valueM, repeatM)
import Streamly.Internal.Data.Stream.Prelude (fromStreamS)
import Streamly.Internal.Data.Stream.StreamD (fromStreamD)
import Streamly.Internal.Data.Stream.StreamK (IsStream((|:), consM))
Expand Down Expand Up @@ -580,3 +581,11 @@ fromCallback setCallback = concatM $ do
{-# INLINE yield #-}
yield :: IsStream t => a -> t m a
yield = value

-- | Same as valueM
--
-- @since 0.4.0
{-# DEPRECATED yieldM "Please use valueM instead." #-}
{-# INLINE yieldM #-}
yieldM :: (Monad m, IsStream t) => m a -> t m a
yieldM = valueM
8 changes: 4 additions & 4 deletions src/Streamly/Internal/Data/Stream/IsStream/Nesting.hs
Expand Up @@ -270,7 +270,7 @@ import Streamly.Internal.Data.Stream.IsStream.Common
, smapM
, splitOnSeq
, value
, yieldM)
, valueM)
import Streamly.Internal.Data.Stream.Parallel (parallel)
import Streamly.Internal.Data.Stream.Prelude
( fromStreamS, toStreamS, concatFoldableWith, concatMapFoldableWith
Expand Down Expand Up @@ -833,7 +833,7 @@ intercalateSuffix seed unf str = fromStreamD $ D.concatUnfold unf
-- Note that 'iterateM' is a special case of 'iterateMapWith':
--
-- @
-- iterateM f = iterateMapWith serial (yieldM . f) . yieldM
-- iterateM f = iterateMapWith serial (valueM . f) . valueM
-- @
--
-- It can be used to traverse a tree structure. For example, to list a
Expand Down Expand Up @@ -893,7 +893,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) (valueM initial)

where

Expand All @@ -902,7 +902,7 @@ iterateSmapMWith combine f initial stream =
feedback b a =
concatMap
(\(b1, s) -> concatMapWith combine (go b1) s)
(yieldM $ f b a)
(valueM $ f b a)

------------------------------------------------------------------------------
-- iterateMap - Either streams
Expand Down
10 changes: 5 additions & 5 deletions src/Streamly/Internal/Data/Stream/Parallel.hs
Expand Up @@ -218,7 +218,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.valueM m `parallel` (toStream r)

infixr 6 `parallel`

Expand All @@ -235,14 +235,14 @@ infixr 6 `parallel`
-- singleton streams. The following trivial example is semantically equivalent
-- to running the action @putStrLn "hello"@ in the current thread:
--
-- >>> Stream.toList $ Stream.yieldM (putStrLn "hello") `Stream.parallel` Stream.nil
-- >>> Stream.toList $ Stream.valueM (putStrLn "hello") `Stream.parallel` Stream.nil
-- hello
-- [()]
--
-- Run two actions concurrently:
--
-- >>> import Control.Concurrent (threadDelay)
-- >>> Stream.toList $ Stream.yieldM (putStrLn "hello") `Stream.parallel` Stream.yieldM (threadDelay 100000 >> putStrLn "world")
-- >>> Stream.toList $ Stream.valueM (putStrLn "hello") `Stream.parallel` Stream.valueM (threadDelay 100000 >> putStrLn "world")
-- hello
-- world
-- [(),()]
Expand All @@ -256,7 +256,7 @@ infixr 6 `parallel`
--
-- Run an effectful action, and a pure effect without any output, concurrently:
--
-- >>> Stream.toList $ Stream.yieldM (return 1) `Stream.parallel` Stream.nilM (putStrLn "world")
-- >>> Stream.toList $ Stream.valueM (return 1) `Stream.parallel` Stream.nilM (putStrLn "world")
-- world
-- [1]
--
Expand Down Expand Up @@ -532,7 +532,7 @@ distributeAsync_ = flip (foldr tapAsync)
--
-- main = S.'drain' . S.'parallely' $ do
-- n <- return 3 \<\> return 2 \<\> return 1
-- S.yieldM $ do
-- S.valueM $ do
-- threadDelay (n * 1000000)
-- myThreadId >>= \\tid -> putStrLn (show tid ++ ": Delay " ++ show n)
-- @
Expand Down
6 changes: 3 additions & 3 deletions src/Streamly/Internal/Data/Stream/Serial.hs
Expand Up @@ -106,7 +106,7 @@ import Prelude hiding (map, mapM, errorWithoutStackTrace)
-- @
-- main = S.drain . 'serially' $ do
-- x <- return 1 \<\> return 2
-- S.yieldM $ print x
-- S.valueM $ print x
-- @
-- @
-- 1
Expand All @@ -119,7 +119,7 @@ import Prelude hiding (map, mapM, errorWithoutStackTrace)
-- main = S.drain . 'serially' $ do
-- x <- return 1 \<\> return 2
-- y <- return 3 \<\> return 4
-- S.yieldM $ print (x, y)
-- S.valueM $ print (x, y)
-- @
-- @
-- (1,3)
Expand Down Expand Up @@ -309,7 +309,7 @@ TRAVERSABLE_INSTANCE(SerialT)
-- main = S.drain . 'wSerially' $ do
-- x <- return 1 \<\> return 2
-- y <- return 3 \<\> return 4
-- S.yieldM $ print (x, y)
-- S.valueM $ print (x, y)
-- @
-- @
-- (1,3)
Expand Down
2 changes: 1 addition & 1 deletion src/Streamly/Internal/Data/Stream/StreamD/Generate.hs
Expand Up @@ -39,7 +39,7 @@ module Streamly.Internal.Data.Stream.StreamD.Generate

-- * From Values
, value
, yieldM
, valueM
, repeat
, repeatM
, replicate
Expand Down
12 changes: 6 additions & 6 deletions src/Streamly/Internal/Data/Stream/StreamD/Type.hs
Expand Up @@ -31,7 +31,7 @@ module Streamly.Internal.Data.Stream.StreamD.Type

-- * From Values
, value
, yieldM
, valueM

-- * From Containers
, fromList
Expand Down Expand Up @@ -196,9 +196,9 @@ value 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 valueM #-}
valueM :: Monad m => m a -> Stream m a
valueM m = Stream step True
where
{-# INLINE_LATE step #-}
step _ True = m >>= \x -> return $ Yield x False
Expand Down Expand Up @@ -364,7 +364,7 @@ foldrS f final (Stream step state) = go SPEC state
{-# INLINE_LATE go #-}
go !_ st = do
-- defState??
r <- yieldM $ step defState st
r <- valueM $ step defState st
case r of
Yield x s -> f x (go SPEC s)
Skip s -> go SPEC s
Expand Down Expand Up @@ -956,7 +956,7 @@ groupsOf2 n input (Fold2 fstep inject extract) (Stream step state) =

instance MonadTrans Stream where
{-# INLINE lift #-}
lift = yieldM
lift = valueM

instance (MonadThrow m) => MonadThrow (Stream m) where
throwM = lift . throwM

0 comments on commit a0ebf77

Please sign in to comment.