on master help sign in
Add scons and snil for Stream construction
d6ec7a2Harendra Kumar
src/Asyncly/Core.hs+12−4
@@ -21,7 +21,12 @@ module Asyncly.Core
21
21
22 -- * Streams
22 -- * Streams
23 , Stream (..)
23 , Stream (..)
24− , yields
24+
25+ -- * Construction
26+ , scons
27+ , snil
28+
29+ -- * Composition
25 , interleave
30 , interleave
26
31
27 -- * Concurrent Stream Vars (SVars)
32 -- * Concurrent Stream Vars (SVars)
@@ -135,6 +140,7 @@ data SVar m a =
135------------------------------------------------------------------------------
140------------------------------------------------------------------------------
136
141
137-- TBD use a functor instead of the bare type a?
142-- TBD use a functor instead of the bare type a?
143+-- XXX remove the Maybe, use "empty" as the base case
138
144
139-- | Represents a monadic stream of values of type 'a' constructed using
145-- | Represents a monadic stream of values of type 'a' constructed using
140-- actions in monad 'm'. Streams can be composed sequentially or in parallel;
146-- actions in monad 'm'. Streams can be composed sequentially or in parallel;
@@ -153,9 +159,11 @@ newtype Stream m a =
153-- | A monad that can perform asynchronous/concurrent IO operations.
159-- | A monad that can perform asynchronous/concurrent IO operations.
154type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
160type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
155
161
156−-- | Yield a singleton value as a stream.
157−yields :: a -> Stream m a
162+scons :: a -> Maybe (Stream m a) -> Stream m a
158−yields a = Stream $ \_ _ yld -> yld a Nothing
163+scons a r = Stream $ \_ _ yld -> yld a r
164+
165+snil :: Stream m a
166+snil = Stream $ \_ stp _ -> stp
159
167
160------------------------------------------------------------------------------
168------------------------------------------------------------------------------
161-- Semigroup
169-- Semigroup
src/Asyncly/Prelude.hs+3−4
@@ -169,11 +169,10 @@ foldlM step begin done m = go begin (toStream m)
169-- | Decompose a stream into its head and tail. If the stream is empty, returns
169-- | Decompose a stream into its head and tail. If the stream is empty, returns
170-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
170-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
171-- the head of the stream and 'ma' its tail.
171-- the head of the stream and 'ma' its tail.
172−uncons :: (Streaming t, Monad m, Monoid (t m a))
172+uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
173− => t m a -> m (Maybe (a, t m a))
174uncons m =
173uncons m =
175 let stop = return Nothing
174 let stop = return Nothing
176− yield a Nothing = return (Just (a, mempty))
175+ yield a Nothing = return (Just (a, nil))
177 yield a (Just x) = return (Just (a, (fromStream x)))
176 yield a (Just x) = return (Just (a, (fromStream x)))
178 in (runStream (toStream m)) Nothing stop yield
177 in (runStream (toStream m)) Nothing stop yield
179
178
@@ -401,7 +400,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
401 yield2 b (Just rb) =
400 yield2 b (Just rb) =
402 (runStream ((g a b) <> (go ra rb))) Nothing stp yld
401 (runStream ((g a b) <> (go ra rb))) Nothing stp yld
403 in (runStream my) Nothing stp yield2
402 in (runStream my) Nothing stp yield2
404− let yield1 a Nothing = merge a mempty
403+ let yield1 a Nothing = merge a snil
405 yield1 a (Just ra) = merge a ra
404 yield1 a (Just ra) = merge a ra
406 (runStream mx) Nothing stp yield1
405 (runStream mx) Nothing stp yield1
407 g a b = toStream $ f a b
406 g a b = toStream $ f a b
src/Asyncly/Streams.hs+9−9
@@ -198,7 +198,7 @@ instance Monad m => Monad (StreamT m) where
198------------------------------------------------------------------------------
198------------------------------------------------------------------------------
199
199
200instance Monad m => Applicative (StreamT m) where
200instance Monad m => Applicative (StreamT m) where
201− pure = StreamT . yields
201+ pure a = StreamT $ scons a Nothing
202 (<*>) = ap
202 (<*>) = ap
203
203
204------------------------------------------------------------------------------
204------------------------------------------------------------------------------
@@ -292,7 +292,7 @@ instance Monad m => Monad (InterleavedT m) where
292------------------------------------------------------------------------------
292------------------------------------------------------------------------------
293
293
294instance Monad m => Applicative (InterleavedT m) where
294instance Monad m => Applicative (InterleavedT m) where
295− pure = InterleavedT . yields
295+ pure a = InterleavedT $ scons a Nothing
296 (<*>) = ap
296 (<*>) = ap
297
297
298------------------------------------------------------------------------------
298------------------------------------------------------------------------------
@@ -403,7 +403,7 @@ instance MonadAsync m => Monad (AsyncT m) where
403------------------------------------------------------------------------------
403------------------------------------------------------------------------------
404
404
405instance MonadAsync m => Applicative (AsyncT m) where
405instance MonadAsync m => Applicative (AsyncT m) where
406− pure = AsyncT . yields
406+ pure a = AsyncT $ scons a Nothing
407 (<*>) = ap
407 (<*>) = ap
408
408
409------------------------------------------------------------------------------
409------------------------------------------------------------------------------
@@ -499,7 +499,7 @@ instance MonadAsync m => Monad (ParallelT m) where
499------------------------------------------------------------------------------
499------------------------------------------------------------------------------
500
500
501instance MonadAsync m => Applicative (ParallelT m) where
501instance MonadAsync m => Applicative (ParallelT m) where
502− pure = ParallelT . yields
502+ pure a = ParallelT $ scons a Nothing
503 (<*>) = ap
503 (<*>) = ap
504
504
505------------------------------------------------------------------------------
505------------------------------------------------------------------------------
@@ -566,7 +566,7 @@ zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
566 let yield2 b Nothing = yld (f a b) Nothing
566 let yield2 b Nothing = yld (f a b) Nothing
567 yield2 b (Just rb) = yld (f a b) (Just (go ra rb))
567 yield2 b (Just rb) = yld (f a b) (Just (go ra rb))
568 in (runStream my) Nothing stp yield2
568 in (runStream my) Nothing stp yield2
569− let yield1 a Nothing = merge a mempty
569+ let yield1 a Nothing = merge a snil
570 yield1 a (Just ra) = merge a ra
570 yield1 a (Just ra) = merge a ra
571 (runStream mx) Nothing stp yield1
571 (runStream mx) Nothing stp yield1
572
572
@@ -585,7 +585,7 @@ instance Monad m => Functor (ZipStream m) where
585 in m Nothing stp yield
585 in m Nothing stp yield
586
586
587instance Monad m => Applicative (ZipStream m) where
587instance Monad m => Applicative (ZipStream m) where
588− pure = ZipStream . yields
588+ pure a = ZipStream $ scons a Nothing
589 (<*>) = zipWith id
589 (<*>) = zipWith id
590
590
591instance Streaming ZipStream where
591instance Streaming ZipStream where
@@ -660,7 +660,7 @@ instance Monad m => Functor (ZipAsync m) where
660 in m Nothing stp yield
660 in m Nothing stp yield
661
661
662instance MonadAsync m => Applicative (ZipAsync m) where
662instance MonadAsync m => Applicative (ZipAsync m) where
663− pure = ZipAsync . yields
663+ pure a = ZipAsync $ scons a Nothing
664 (<*>) = zipAsyncWith id
664 (<*>) = zipAsyncWith id
665
665
666instance Streaming ZipAsync where
666instance Streaming ZipAsync where
@@ -744,10 +744,10 @@ zippingAsync x = x
744------------------------------------------------------------------------------
744------------------------------------------------------------------------------
745
745
746cons :: (Streaming t) => a -> t m a -> t m a
746cons :: (Streaming t) => a -> t m a -> t m a
747−cons a r = fromStream $ Stream $ \_ _ yld -> yld a (Just (toStream r))
747+cons a r = fromStream $ scons a (Just (toStream r))
748
748
749nil :: Streaming t => t m a
749nil :: Streaming t => t m a
750−nil = fromStream $ Stream $ \_ stp _ -> stp
750+nil = fromStream $ snil
751
751
752-- | Build a stream from its church encoding. The function passed maps
752-- | Build a stream from its church encoding. The function passed maps
753-- directly to the underlying representation of the stream type. The second
753-- directly to the underlying representation of the stream type. The second