on master help sign in
Add unfoldr
1f4932dHarendra Kumar
src/Asyncly.hs+1
@@ -128,6 +128,7 @@ module Asyncly
128 , toList
128 , toList
129 , uncons
129 , uncons
130 , async
130 , async
131+ , unfoldr
131
132
132 -- * Monadic Composition (Conjunction)
133 -- * Monadic Composition (Conjunction)
133 -- $bind
134 -- $bind
src/Asyncly/AsyncT.hs+13−2
@@ -24,6 +24,7 @@ module Asyncly.AsyncT
24 , toList
24 , toList
25 , uncons
25 , uncons
26 , async
26 , async
27+ , unfoldr
27
28
28 , take
29 , take
29 , drop
30 , drop
@@ -717,6 +718,16 @@ uncons m = (runAsyncT m) Nothing stop yield
717 yield a Nothing = return (Just (a, empty))
718 yield a Nothing = return (Just (a, empty))
718 yield a (Just x) = return (Just (a, x))
719 yield a (Just x) = return (Just (a, x))
719
720
721+-- | Build a Stream by unfolding steps starting from a seed.
722+unfoldr :: MonadAsync m => (b -> m (Maybe (a, b))) -> b -> AsyncT m a
723+unfoldr step = go
724+ where
725+ go s = AsyncT $ \_ stp yld -> do
726+ mayb <- step s
727+ case mayb of
728+ Nothing -> stp
729+ Just (a, b) -> yld a (Just (go b))
730+
720------------------------------------------------------------------------------
731------------------------------------------------------------------------------
721-- Transformation
732-- Transformation
722------------------------------------------------------------------------------
733------------------------------------------------------------------------------
@@ -769,7 +780,7 @@ zipWith f m1 m2 = AsyncT $ \_ stp yld -> do
769 (runAsyncT m1) Nothing stp yield1
780 (runAsyncT m1) Nothing stp yield1
770
781
771-- | Wrapper around AsyncT type with a serial zipping Applicative instance.
782-- | Wrapper around AsyncT type with a serial zipping Applicative instance.
772−-- Note that the binary function interleave (<=>) is a special case of
783+-- Note that the binary function interleave (\<=>) is a special case of
773-- ZipSerial Applicative.
784-- ZipSerial Applicative.
774--
785--
775-- > f <$> ZipSerial xs1 <*> ... <*> ZipSerial xsN
786-- > f <$> ZipSerial xs1 <*> ... <*> ZipSerial xsN
@@ -835,7 +846,7 @@ zipAsyncWith f m1 m2 = AsyncT $ \_ stp yld -> do
835 (runAsyncT (zipWith f ma mb)) Nothing stp yld
846 (runAsyncT (zipWith f ma mb)) Nothing stp yld
836
847
837-- | Wrapper around AsyncT type with a parallel zipping Applicative instance.
848-- | Wrapper around AsyncT type with a parallel zipping Applicative instance.
838−-- Note that the binary operator (<|>) from the Alternative instance of AsyncT
849+-- Note that the binary operator (\<|>) from the Alternative instance of AsyncT
839-- is a special case of ZipAsync Applicative.
850-- is a special case of ZipAsync Applicative.
840--
851--
841-- > f <$> ZipAsync xs1 <*> ... <*> ZipAsync xsN
852-- > f <$> ZipAsync xs1 <*> ... <*> ZipAsync xsN