on bc0da07 help sign in
129 lines 3.0 kB history raw
1-- |
2-- Module : Streamly.Internal.Data.Stream.Serial
3-- Copyright : (c) 2017 Composewell Technologies
4--
5-- License : BSD3
6-- Maintainer : streamly@composewell.com
7-- Stability : experimental
8-- Portability : GHC
9--
10-- To run examples in this module:
11--
12-- >>> import qualified Streamly.Prelude as Stream
13--
14module Streamly.Internal.Data.Stream.Serial
15 (
16 -- * Serial appending stream
17 SerialT
18 , Serial
19 , serial
21 -- * Construction
22 , Stream.cons
23 , Stream.consM
24 , repeat
25 , unfoldrM
26 , fromList
28 -- * Elimination
29 , toList
31 -- * Transformation
32 , map
33 , Stream.mapM
34 )
35where
37#if __GLASGOW_HASKELL__ < 808
38import Data.Semigroup (Semigroup(..))
39#endif
40import GHC.Exts (IsList(..))
42import qualified Streamly.Internal.Data.Stream.StreamD.Generate as D
43import qualified Streamly.Internal.Data.Stream as Stream
45import Prelude hiding (map, mapM, repeat)
47#include "inline.hs"
49-- $setup
50-- >>> import qualified Streamly.Prelude as Stream
52------------------------------------------------------------------------------
53-- SerialT
54------------------------------------------------------------------------------
56type SerialT = Stream.Stream
58-- | A serial IO stream of elements of type @a@. See 'SerialT' documentation
59-- for more details.
60--
61-- /Since: 0.2.0 ("Streamly")/
62--
63-- @since 0.8.0
64type Serial = SerialT IO
66------------------------------------------------------------------------------
67-- Generation
68------------------------------------------------------------------------------
70-- |
71-- Generate an infinite stream by repeating a pure value.
72--
73{-# INLINE_NORMAL repeat #-}
74repeat :: Monad m => a -> SerialT m a
75repeat = Stream.fromStreamD . D.repeat
77------------------------------------------------------------------------------
78-- Combining
79------------------------------------------------------------------------------
81{-# INLINE serial #-}
82serial :: SerialT m a -> SerialT m a -> SerialT m a
83serial = (<>)
85-- |
86-- @
87-- map = fmap
88-- @
89--
90-- Same as 'fmap'.
91--
92-- @
93-- > S.toList $ S.map (+1) $ S.fromList [1,2,3]
94-- [2,3,4]
95-- @
96--
97-- @since 0.4.0
98{-# INLINE map #-}
99map :: Monad m => (a -> b) -> SerialT m a -> SerialT m b
100map f = Stream.mapM (return . f)
102------------------------------------------------------------------------------
103-- Construction
104------------------------------------------------------------------------------
106-- | Build a stream by unfolding a /monadic/ step function starting from a
107-- seed. The step function returns the next element in the stream and the next
108-- seed value. When it is done it returns 'Nothing' and the stream ends. For
109-- example,
110--
111-- @
112-- let f b =
113-- if b > 3
114-- then return Nothing
115-- else print b >> return (Just (b, b + 1))
116-- in drain $ unfoldrM f 0
117-- @
118-- @
119-- 0
120-- 1
121-- 2
122-- 3
123-- @
124--
125-- /Pre-release/
126--
127{-# INLINE unfoldrM #-}
128unfoldrM :: Monad m => (b -> m (Maybe (a, b))) -> b -> SerialT m a
129unfoldrM step seed = Stream.fromStreamD (D.unfoldrM step seed)