on master help sign in
Add scons and snil for Stream construction
commitd6ec7a2857478901fda7474f329dfda84bc6d60a authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 31 Oct 2017, 16:05 (9 years ago) parent9588d8f
3 files changed+24−17
src/Asyncly/Core.hs+12−4 src/Asyncly/Prelude.hs+3−4 src/Asyncly/Streams.hs+9−9
src/Asyncly/Core.hs+12−4
@@ -21,7 +21,12 @@ module Asyncly.Core
2121
2222 -- * Streams
2323 , Stream (..)
24− , yields
24+
25+ -- * Construction
26+ , scons
27+ , snil
28+
29+ -- * Composition
2530 , interleave
2631
2732 -- * Concurrent Stream Vars (SVars)
@@ -135,6 +140,7 @@ data SVar m a =
135140------------------------------------------------------------------------------
136141
137142-- TBD use a functor instead of the bare type a?
143+-- XXX remove the Maybe, use "empty" as the base case
138144
139145-- | Represents a monadic stream of values of type 'a' constructed using
140146-- actions in monad 'm'. Streams can be composed sequentially or in parallel;
@@ -153,9 +159,11 @@ newtype Stream m a =
153159-- | A monad that can perform asynchronous/concurrent IO operations.
154160type MonadAsync m = (MonadIO m, MonadBaseControl IO m, MonadThrow m)
155161
156−-- | Yield a singleton value as a stream.
157−yields :: a -> Stream m a
158−yields a = Stream $ \_ _ yld -> yld a Nothing
162+scons :: a -> Maybe (Stream m a) -> Stream m a
163+scons a r = Stream $ \_ _ yld -> yld a r
164+
165+snil :: Stream m a
166+snil = Stream $ \_ stp _ -> stp
159167
160168------------------------------------------------------------------------------
161169-- Semigroup
src/Asyncly/Prelude.hs+3−4
@@ -169,11 +169,10 @@ foldlM step begin done m = go begin (toStream m)
169169-- | Decompose a stream into its head and tail. If the stream is empty, returns
170170-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
171171-- the head of the stream and 'ma' its tail.
172−uncons :: (Streaming t, Monad m, Monoid (t m a))
173− => t m a -> m (Maybe (a, t m a))
172+uncons :: (Streaming t, Monad m) => t m a -> m (Maybe (a, t m a))
174173uncons m =
175174 let stop = return Nothing
176− yield a Nothing = return (Just (a, mempty))
175+ yield a Nothing = return (Just (a, nil))
177176 yield a (Just x) = return (Just (a, (fromStream x)))
178177 in (runStream (toStream m)) Nothing stop yield
179178
@@ -401,7 +400,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
401400 yield2 b (Just rb) =
402401 (runStream ((g a b) <> (go ra rb))) Nothing stp yld
403402 in (runStream my) Nothing stp yield2
404− let yield1 a Nothing = merge a mempty
403+ let yield1 a Nothing = merge a snil
405404 yield1 a (Just ra) = merge a ra
406405 (runStream mx) Nothing stp yield1
407406 g a b = toStream $ f a b
src/Asyncly/Streams.hs+9−9
@@ -198,7 +198,7 @@ instance Monad m => Monad (StreamT m) where
198198------------------------------------------------------------------------------
199199
200200instance Monad m => Applicative (StreamT m) where
201− pure = StreamT . yields
201+ pure a = StreamT $ scons a Nothing
202202 (<*>) = ap
203203
204204------------------------------------------------------------------------------
@@ -292,7 +292,7 @@ instance Monad m => Monad (InterleavedT m) where
292292------------------------------------------------------------------------------
293293
294294instance Monad m => Applicative (InterleavedT m) where
295− pure = InterleavedT . yields
295+ pure a = InterleavedT $ scons a Nothing
296296 (<*>) = ap
297297
298298------------------------------------------------------------------------------
@@ -403,7 +403,7 @@ instance MonadAsync m => Monad (AsyncT m) where
403403------------------------------------------------------------------------------
404404
405405instance MonadAsync m => Applicative (AsyncT m) where
406− pure = AsyncT . yields
406+ pure a = AsyncT $ scons a Nothing
407407 (<*>) = ap
408408
409409------------------------------------------------------------------------------
@@ -499,7 +499,7 @@ instance MonadAsync m => Monad (ParallelT m) where
499499------------------------------------------------------------------------------
500500
501501instance MonadAsync m => Applicative (ParallelT m) where
502− pure = ParallelT . yields
502+ pure a = ParallelT $ scons a Nothing
503503 (<*>) = ap
504504
505505------------------------------------------------------------------------------
@@ -566,7 +566,7 @@ zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
566566 let yield2 b Nothing = yld (f a b) Nothing
567567 yield2 b (Just rb) = yld (f a b) (Just (go ra rb))
568568 in (runStream my) Nothing stp yield2
569− let yield1 a Nothing = merge a mempty
569+ let yield1 a Nothing = merge a snil
570570 yield1 a (Just ra) = merge a ra
571571 (runStream mx) Nothing stp yield1
572572
@@ -585,7 +585,7 @@ instance Monad m => Functor (ZipStream m) where
585585 in m Nothing stp yield
586586
587587instance Monad m => Applicative (ZipStream m) where
588− pure = ZipStream . yields
588+ pure a = ZipStream $ scons a Nothing
589589 (<*>) = zipWith id
590590
591591instance Streaming ZipStream where
@@ -660,7 +660,7 @@ instance Monad m => Functor (ZipAsync m) where
660660 in m Nothing stp yield
661661
662662instance MonadAsync m => Applicative (ZipAsync m) where
663− pure = ZipAsync . yields
663+ pure a = ZipAsync $ scons a Nothing
664664 (<*>) = zipAsyncWith id
665665
666666instance Streaming ZipAsync where
@@ -744,10 +744,10 @@ zippingAsync x = x
744744------------------------------------------------------------------------------
745745
746746cons :: (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))
748748
749749nil :: Streaming t => t m a
750−nil = fromStream $ Stream $ \_ stp _ -> stp
750+nil = fromStream $ snil
751751
752752-- | Build a stream from its church encoding. The function passed maps
753753-- directly to the underlying representation of the stream type. The second