on master help sign in
Add (.:) operator for constructing streams like lists
f94678fHarendra Kumar
src/Streamly/Prelude.hs+3−2
@@ -18,8 +18,9 @@
18module Streamly.Prelude
18module Streamly.Prelude
19 (
19 (
20 -- * Construction
20 -- * Construction
21+ nil
21− cons
22+ , cons
22− , nil
23+ , (.:)
23 , unfoldr
24 , unfoldr
24 , unfoldrM
25 , unfoldrM
25 , each
26 , each
src/Streamly/Streams.hs+32−6
@@ -29,13 +29,14 @@ module Streamly.Streams
29 , newEmptySVar
29 , newEmptySVar
30
30
31 -- * Construction
31 -- * Construction
32+ , nil
33+ , cons
34+ , (.:)
32 , streamBuild
35 , streamBuild
33 , fromCallback
36 , fromCallback
34 , fromSVar
37 , fromSVar
35
38
36 -- * Elimination
39 -- * Elimination
37− , cons
38− , nil
39 , streamFold
40 , streamFold
40 , runStreaming
41 , runStreaming
41 , toSVar
42 , toSVar
@@ -111,13 +112,38 @@ class Streaming t where
111-- Constructing a stream
112-- Constructing a stream
112------------------------------------------------------------------------------
113------------------------------------------------------------------------------
113
114
114−-- | Add an element a the head of a stream.
115+-- | Represesnts an empty stream just like @[]@ represents and empty list.
116+nil :: Streaming t => t m a
117+nil = fromStream snil
118+
119+infixr 5 `cons`
120+
121+-- | Constructs a stream by adding a pure value at the head of an existing
122+-- stream, just like ':' constructs lists. For example:
123+--
124+-- @
125+-- > let stream = 1 \`cons` 2 \`cons` 3 \`cons` nil
126+-- > (toList . serially) stream
127+-- [1,2,3]
128+-- @
115cons :: (Streaming t) => a -> t m a -> t m a
129cons :: (Streaming t) => a -> t m a -> t m a
116cons a r = fromStream $ scons a (Just (toStream r))
130cons a r = fromStream $ scons a (Just (toStream r))
117
131
118−-- | An empty stream.
132+infixr 5 .:
133+
134+-- | Operator equivalent of 'cons' so that you can construct a stream of pure
135+-- values more succinctly like this:
136+--
137+-- @
138+-- > let stream = 1 .: 2 .: 3 .: nil
139+-- > (toList . serially) stream
140+-- [1,2,3]
141+-- @
142+--
143+-- '.:' constructs a stream just like ':' constructs a list.
144+--
119−nil :: Streaming t => t m a
145+(.:) :: (Streaming t) => a -> t m a -> t m a
120−nil = fromStream snil
146+(.:) = cons
121
147
122-- | Build a stream from its church encoding. The function passed maps
148-- | Build a stream from its church encoding. The function passed maps
123-- directly to the underlying representation of the stream type. The second
149-- directly to the underlying representation of the stream type. The second