-- | -- Module : Streamly.Internal.Data.Stream.Serial -- Copyright : (c) 2017 Composewell Technologies -- -- License : BSD3 -- Maintainer : streamly@composewell.com -- Stability : experimental -- Portability : GHC -- -- To run examples in this module: -- -- >>> import qualified Streamly.Prelude as Stream -- module Streamly.Internal.Data.Stream.Serial ( -- * Serial appending stream SerialT , Serial , serial -- * Construction , Stream.cons , Stream.consM , repeat , unfoldrM , fromList -- * Elimination , toList -- * Transformation , map , Stream.mapM ) where #if __GLASGOW_HASKELL__ < 808 import Data.Semigroup (Semigroup(..)) #endif import GHC.Exts (IsList(..)) import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D import qualified Streamly.Internal.Data.Stream as Stream import Prelude hiding (map, mapM, repeat) #include "inline.hs" -- $setup -- >>> import qualified Streamly.Prelude as Stream ------------------------------------------------------------------------------ -- SerialT ------------------------------------------------------------------------------ type SerialT = Stream.Stream -- | A serial IO stream of elements of type @a@. See 'SerialT' documentation -- for more details. -- -- /Since: 0.2.0 ("Streamly")/ -- -- @since 0.8.0 type Serial = SerialT IO ------------------------------------------------------------------------------ -- Generation ------------------------------------------------------------------------------ -- | -- Generate an infinite stream by repeating a pure value. -- {-# INLINE_NORMAL repeat #-} repeat :: Monad m => a -> SerialT m a repeat = Stream.fromStreamD . D.repeat ------------------------------------------------------------------------------ -- Combining ------------------------------------------------------------------------------ {-# INLINE serial #-} serial :: SerialT m a -> SerialT m a -> SerialT m a serial = (<>) -- | -- @ -- map = fmap -- @ -- -- Same as 'fmap'. -- -- @ -- > S.toList $ S.map (+1) $ S.fromList [1,2,3] -- [2,3,4] -- @ -- -- @since 0.4.0 {-# INLINE map #-} map :: Monad m => (a -> b) -> SerialT m a -> SerialT m b map f = Stream.mapM (return . f) ------------------------------------------------------------------------------ -- Construction ------------------------------------------------------------------------------ -- | Build a stream by unfolding a /monadic/ step function starting from a -- seed. The step function returns the next element in the stream and the next -- seed value. When it is done it returns 'Nothing' and the stream ends. For -- example, -- -- @ -- let f b = -- if b > 3 -- then return Nothing -- else print b >> return (Just (b, b + 1)) -- in drain $ unfoldrM f 0 -- @ -- @ -- 0 -- 1 -- 2 -- 3 -- @ -- -- /Pre-release/ -- {-# INLINE unfoldrM #-} unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a unfoldrM step seed = Stream.fromStreamD (D.unfoldrM step seed)