on master help sign in
Move the StreamD Type in a separate module
e08ff86Harendra Kumar
src/Streamly/Streams/StreamD.hs+2−63
@@ -199,47 +199,11 @@ import Prelude
199 notElem, null, head, tail, zipWith, lookup, foldr1, sequence,
199 notElem, null, head, tail, zipWith, lookup, foldr1, sequence,
200 (!!), scanl, scanl1, concatMap, replicate, enumFromTo)
200 (!!), scanl, scanl1, concatMap, replicate, enumFromTo)
201
201
202−import Streamly.SVar (MonadAsync, State(..), defState, adaptState)
202+import Streamly.SVar (MonadAsync, defState, adaptState)
203
203
204+import Streamly.Streams.StreamDType
204import qualified Streamly.Streams.StreamK as K
205import qualified Streamly.Streams.StreamK as K
205
206
206−------------------------------------------------------------------------------
207−-- The direct style stream type
208−------------------------------------------------------------------------------
209−
210−-- | A stream is a succession of 'Step's. A 'Yield' produces a single value and
211−-- the next state of the stream. 'Stop' indicates there are no more values in
212−-- the stream.
213−data Step s a = Yield a s | Skip s | Stop
214−
215−instance Functor (Step s) where
216− {-# INLINE fmap #-}
217− fmap f (Yield x s) = Yield (f x) s
218− fmap _ (Skip s) = Skip s
219− fmap _ Stop = Stop
220−
221−-- gst = global state
222−-- | A stream consists of a step function that generates the next step given a
223−-- current state, and the current state.
224−data Stream m a =
225− forall s. UnStream (State K.Stream m a -> s -> m (Step s a)) s
226−
227−-- Do not use the UnStream constructor directly, use the pattern synonym
228−-- instead. XXX we should perhaps put the type in a separate module and do not
229−-- export the constructor.
230−
231−detachStream :: Stream m a -> Stream m a
232−detachStream (UnStream step state) = UnStream step' state
233− where step' gst = step (adaptState gst)
234−
235−pattern Stream :: (State K.Stream m a -> s -> m (Step s a)) -> s -> Stream m a
236−pattern Stream step state <- (detachStream -> UnStream step state)
237− where Stream = UnStream
238−
239−#if __GLASGOW_HASKELL__ >= 802
240−{-# COMPLETE Stream #-}
241−#endif
242−
243------------------------------------------------------------------------------
207------------------------------------------------------------------------------
244-- Construction
208-- Construction
245------------------------------------------------------------------------------
209------------------------------------------------------------------------------
@@ -1212,23 +1176,6 @@ uniq (Stream step state) = Stream step' (Nothing, state)
1212-- Transformation by Mapping
1176-- Transformation by Mapping
1213------------------------------------------------------------------------------
1177------------------------------------------------------------------------------
1214
1178
1215−-- | Map a monadic function over a 'Stream'
1216−{-# INLINE_NORMAL mapM #-}
1217−mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
1218−mapM f (Stream step state) = Stream step' state
1219− where
1220− {-# INLINE_LATE step' #-}
1221− step' gst st = do
1222− r <- step (adaptState gst) st
1223− case r of
1224− Yield x s -> f x >>= \a -> return $ Yield a s
1225− Skip s -> return $ Skip s
1226− Stop -> return Stop
1227−
1228−{-# INLINE map #-}
1229−map :: Monad m => (a -> b) -> Stream m a -> Stream m b
1230−map f = mapM (return . f)
1231−
1232{-# INLINE_NORMAL sequence #-}
1179{-# INLINE_NORMAL sequence #-}
1233sequence :: Monad m => Stream m (m a) -> Stream m a
1180sequence :: Monad m => Stream m (m a) -> Stream m a
1234sequence (Stream step state) = Stream step' state
1181sequence (Stream step state) = Stream step' state
@@ -1497,11 +1444,3 @@ the (Stream step state) = go state
1497 | otherwise -> return Nothing
1444 | otherwise -> return Nothing
1498 Skip s -> go' n s
1445 Skip s -> go' n s
1499 Stop -> return (Just n)
1446 Stop -> return (Just n)
1500−
1501−------------------------------------------------------------------------------
1502−-- Instances
1503−------------------------------------------------------------------------------
1504−
1505−instance Monad m => Functor (Stream m) where
1506− {-# INLINE fmap #-}
1507− fmap = map
src/Streamly/Streams/StreamDType.hsnew file+98
@@ -0,0 +1,98 @@
1+{-# LANGUAGE BangPatterns #-}
2+{-# LANGUAGE CPP #-}
3+{-# LANGUAGE ConstraintKinds #-}
4+{-# LANGUAGE ExistentialQuantification #-}
5+{-# LANGUAGE FlexibleContexts #-}
6+{-# LANGUAGE FlexibleInstances #-}
7+{-# LANGUAGE MultiParamTypeClasses #-}
8+{-# LANGUAGE PatternSynonyms #-}
9+{-# LANGUAGE ViewPatterns #-}
10+{-# LANGUAGE RankNTypes #-}
11+
12+#include "inline.hs"
13+
14+-- |
15+-- Module : Streamly.Streams.StreamDType
16+-- Copyright : (c) 2018 Harendra Kumar
17+--
18+-- License : BSD3
19+-- Maintainer : harendra.kumar@gmail.com
20+-- Stability : experimental
21+-- Portability : GHC
22+
23+module Streamly.Streams.StreamDType
24+ (
25+ -- * The stream type
26+ Step (..)
27+#if __GLASGOW_HASKELL__ >= 800
28+ , Stream (Stream)
29+#else
30+ , Stream ()
31+ , pattern Stream
32+#endif
33+ , map
34+ , mapM
35+ )
36+where
37+
38+import Streamly.SVar (State(..), adaptState)
39+import qualified Streamly.Streams.StreamK as K
40+import Prelude hiding (map, mapM)
41+
42+------------------------------------------------------------------------------
43+-- The direct style stream type
44+------------------------------------------------------------------------------
45+
46+-- | A stream is a succession of 'Step's. A 'Yield' produces a single value and
47+-- the next state of the stream. 'Stop' indicates there are no more values in
48+-- the stream.
49+data Step s a = Yield a s | Skip s | Stop
50+
51+instance Functor (Step s) where
52+ {-# INLINE fmap #-}
53+ fmap f (Yield x s) = Yield (f x) s
54+ fmap _ (Skip s) = Skip s
55+ fmap _ Stop = Stop
56+
57+-- gst = global state
58+-- | A stream consists of a step function that generates the next step given a
59+-- current state, and the current state.
60+data Stream m a =
61+ forall s. UnStream (State K.Stream m a -> s -> m (Step s a)) s
62+
63+detachStream :: Stream m a -> Stream m a
64+detachStream (UnStream step state) = UnStream step' state
65+ where step' gst = step (adaptState gst)
66+
67+pattern Stream :: (State K.Stream m a -> s -> m (Step s a)) -> s -> Stream m a
68+pattern Stream step state <- (detachStream -> UnStream step state)
69+ where Stream = UnStream
70+
71+#if __GLASGOW_HASKELL__ >= 802
72+{-# COMPLETE Stream #-}
73+#endif
74+
75+------------------------------------------------------------------------------
76+-- Instances
77+------------------------------------------------------------------------------
78+
79+-- | Map a monadic function over a 'Stream'
80+{-# INLINE_NORMAL mapM #-}
81+mapM :: Monad m => (a -> m b) -> Stream m a -> Stream m b
82+mapM f (Stream step state) = Stream step' state
83+ where
84+ {-# INLINE_LATE step' #-}
85+ step' gst st = do
86+ r <- step (adaptState gst) st
87+ case r of
88+ Yield x s -> f x >>= \a -> return $ Yield a s
89+ Skip s -> return $ Skip s
90+ Stop -> return Stop
91+
92+{-# INLINE map #-}
93+map :: Monad m => (a -> b) -> Stream m a -> Stream m b
94+map f = mapM (return . f)
95+
96+instance Monad m => Functor (Stream m) where
97+ {-# INLINE fmap #-}
98+ fmap = map
streamly.cabal+1
@@ -129,6 +129,7 @@ library
129
129
130 -- Base streams
130 -- Base streams
131 , Streamly.Streams.StreamK
131 , Streamly.Streams.StreamK
132+ , Streamly.Streams.StreamDType
132 , Streamly.Streams.StreamD
133 , Streamly.Streams.StreamD
133 , Streamly.Streams.Prelude
134 , Streamly.Streams.Prelude
134
135