on master help sign in
Add pure unfoldr
b1e400bHarendra Kumar
src/Asyncly/Prelude.hs+17−6
@@ -23,6 +23,7 @@ module Asyncly.Prelude
23 cons
23 cons
24 , nil
24 , nil
25 , unfoldr
25 , unfoldr
26+ , unfoldrM
26 , each
27 , each
27 , fromHandle
28 , fromHandle
28
29
@@ -87,9 +88,18 @@ import Asyncly.Streams
87-- Construction
88-- Construction
88------------------------------------------------------------------------------
89------------------------------------------------------------------------------
89
90
90−-- | Build a Stream by unfolding steps starting from a seed.
91+-- | Build a Stream by unfolding pure steps starting from a seed.
91−unfoldr :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
92+unfoldr :: Streaming t => (b -> Maybe (a, b)) -> b -> t m a
92unfoldr step = fromStream . go
93unfoldr step = fromStream . go
94+ where
95+ go s = Stream $ \_ stp yld -> do
96+ case step s of
97+ Nothing -> stp
98+ Just (a, b) -> yld a (Just (go b))
99+
100+-- | Build a Stream by unfolding monadic steps starting from a seed.
101+unfoldrM :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
102+unfoldrM step = fromStream . go
93 where
103 where
94 go s = Stream $ \_ stp yld -> do
104 go s = Stream $ \_ stp yld -> do
95 mayb <- step s
105 mayb <- step s
@@ -354,8 +364,8 @@ maximum m = go Nothing (toStream m)
354
364
355-- XXX Parallel variants of these? mapMWith et al. sequenceWith.
365-- XXX Parallel variants of these? mapMWith et al. sequenceWith.
356
366
357−-- | Apply a monadic action to each element of the stream and replace it with
367+-- | Replace each element of the stream with the result of a monadic action
358−-- the result.
368+-- applied on the element.
359mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
369mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
360mapM f m = fromStream $ go (toStream m)
370mapM f m = fromStream $ go (toStream m)
361 where
371 where
@@ -366,7 +376,7 @@ mapM f m = fromStream $ go (toStream m)
366 in (runStream m1) Nothing stop yield
376 in (runStream m1) Nothing stop yield
367
377
368-- | Apply a monadic action to each element of the stream and discard the
378-- | Apply a monadic action to each element of the stream and discard the
369−-- results.
379+-- output of the action.
370mapM_ :: (Streaming t, Monad m) => (a -> m b) -> t m a -> m ()
380mapM_ :: (Streaming t, Monad m) => (a -> m b) -> t m a -> m ()
371mapM_ f m = go (toStream m)
381mapM_ f m = go (toStream m)
372 where
382 where
@@ -376,7 +386,8 @@ mapM_ f m = go (toStream m)
376 yield a (Just x) = f a >> go x
386 yield a (Just x) = f a >> go x
377 in (runStream m1) Nothing stop yield
387 in (runStream m1) Nothing stop yield
378
388
379−-- | Reduce a stream of monadic actions to a stream of their results.
389+-- | Reduce a stream of monadic actions to a stream of the output of those
390+-- actions.
380sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
391sequence :: (Streaming t, Monad m) => t m (m a) -> t m a
381sequence m = fromStream $ go (toStream m)
392sequence m = fromStream $ go (toStream m)
382 where
393 where