on master help sign in
Add foldr
7689ff5Harendra Kumar
src/Asyncly/Core.hs+1−2
@@ -640,8 +640,7 @@ m1 <| m2 = fromStream $ parLeft (toStream m1) (toStream m2)
640-- Running the monad
640-- Running the monad
641------------------------------------------------------------------------------
641------------------------------------------------------------------------------
642
642
643−-- | Run a composed streaming computation, wait for it to finish and discard
643+-- | Run a composed streaming computation, discard the results.
644−-- the results.
645runStreaming :: (Monad m, Streaming t) => t m a -> m ()
644runStreaming :: (Monad m, Streaming t) => t m a -> m ()
646runStreaming m = go (toStream m)
645runStreaming m = go (toStream m)
647
646
src/Asyncly/Prelude.hs+18−13
@@ -24,10 +24,11 @@ module Asyncly.Prelude
24
24
25 -- * Elimination
25 -- * Elimination
26 , runStreaming
26 , runStreaming
27− , toList
27+ , foldr
28 , uncons
28 , uncons
29
29
30 -- * Special folds
30 -- * Special folds
31+ , toList
31 , take
32 , take
32 , drop
33 , drop
33
34
@@ -41,27 +42,26 @@ where
41
42
42import Control.Monad (liftM)
43import Control.Monad (liftM)
43import Data.Semigroup (Semigroup(..))
44import Data.Semigroup (Semigroup(..))
44−import Prelude hiding (drop, take, zipWith)
45+import Prelude hiding (drop, take, zipWith, foldr)
45import Asyncly.Core
46import Asyncly.Core
46
47
47------------------------------------------------------------------------------
48------------------------------------------------------------------------------
48-- Elimination
49-- Elimination
49------------------------------------------------------------------------------
50------------------------------------------------------------------------------
50
51
51−-- | Collect the results of an 'AsyncT' stream into a list.
52+-- | Right fold a stream producing a result in the underlying monad.
52−{-# INLINABLE toList #-}
53−toList :: (Monad m, Streaming t) => t m a -> m [a]
53+foldr :: Streaming t => (a -> m b -> m b) -> m b -> t m a -> m b
54−toList m = go (toStream m)
54+foldr f z m = go (toStream m)
55
55
56 where
56 where
57
57
58 go m1 = (runStream m1) Nothing stop yield
58 go m1 = (runStream m1) Nothing stop yield
59
59
60− stop = return []
60+ stop = z
61
61
62 {-# INLINE yield #-}
62 {-# INLINE yield #-}
63− yield a Nothing = return [a]
63+ yield a Nothing = f a z
64− yield a (Just x) = liftM (a :) (go x)
64+ yield a (Just x) = f a (go x)
65
65
66-- | Decompose a stream into its head and tail. If the stream is empty, returns
66-- | Decompose a stream into its head and tail. If the stream is empty, returns
67-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
67-- 'Nothing'. If the stream is non-empty, returns 'Just (a, ma)', where 'a' is
@@ -98,6 +98,11 @@ fromCallback k = Stream $ \_ _ yld -> k (\a -> yld a Nothing)
98-- Special folds
98-- Special folds
99------------------------------------------------------------------------------
99------------------------------------------------------------------------------
100
100
101+-- | Convert a stream into a list in the underlying monad.
102+{-# INLINABLE toList #-}
103+toList :: (Monad m, Streaming t) => t m a -> m [a]
104+toList = foldr (\a xs -> liftM (a :) xs) (return [])
105+
101-- | Take first 'n' elements from the stream and discard the rest.
106-- | Take first 'n' elements from the stream and discard the rest.
102take :: Streaming t => Int -> t m a -> t m a
107take :: Streaming t => Int -> t m a -> t m a
103take n m = fromStream $ go n (toStream m)
108take n m = fromStream $ go n (toStream m)
@@ -124,7 +129,7 @@ drop n m = fromStream $ go n (toStream m)
124-- Serially Zipping Streams
129-- Serially Zipping Streams
125------------------------------------------------------------------------------
130------------------------------------------------------------------------------
126
131
127−-- | Zip two AsyncT streams serially using a monadic zipping function.
132+-- | Zip two streams serially using a monadic zipping function.
128zipWithM :: Streaming t => (a -> b -> t m c) -> t m a -> t m b -> t m c
133zipWithM :: Streaming t => (a -> b -> t m c) -> t m a -> t m b -> t m c
129zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
134zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
130 where
135 where
@@ -139,7 +144,7 @@ zipWithM f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
139 (runStream mx) Nothing stp yield1
144 (runStream mx) Nothing stp yield1
140 g a b = toStream $ f a b
145 g a b = toStream $ f a b
141
146
142−-- | Zip two AsyncT streams serially using a pure zipping function.
147+-- | Zip two streams serially using a pure zipping function.
143zipWith :: Streaming t => (a -> b -> c) -> t m a -> t m b -> t m c
148zipWith :: Streaming t => (a -> b -> c) -> t m a -> t m b -> t m c
144zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
149zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
145 where
150 where
@@ -156,7 +161,7 @@ zipWith f m1 m2 = fromStream $ go (toStream m1) (toStream m2)
156-- Parallely Zipping Streams
161-- Parallely Zipping Streams
157------------------------------------------------------------------------------
162------------------------------------------------------------------------------
158
163
159−-- | Zip two AsyncT streams asyncly (i.e. both the streams are generated
164+-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
160-- concurrently) using a monadic zipping function.
165-- concurrently) using a monadic zipping function.
161zipAsyncWithM :: (Streaming t, MonadAsync m)
166zipAsyncWithM :: (Streaming t, MonadAsync m)
162 => (a -> b -> t m c) -> t m a -> t m b -> t m c
167 => (a -> b -> t m c) -> t m a -> t m b -> t m c
@@ -165,7 +170,7 @@ zipAsyncWithM f m1 m2 = fromStream $ Stream $ \_ stp yld -> do
165 mb <- async m2
170 mb <- async m2
166 (runStream (toStream (zipWithM f ma mb))) Nothing stp yld
171 (runStream (toStream (zipWithM f ma mb))) Nothing stp yld
167
172
168−-- | Zip two AsyncT streams asyncly (i.e. both the streams are generated
173+-- | Zip two streams asyncly (i.e. both the elements being zipped are generated
169-- concurrently) using a pure zipping function.
174-- concurrently) using a pure zipping function.
170zipAsyncWith :: (Streaming t, MonadAsync m)
175zipAsyncWith :: (Streaming t, MonadAsync m)
171 => (a -> b -> c) -> t m a -> t m b -> t m c
176 => (a -> b -> c) -> t m a -> t m b -> t m c