@@ -20,7 +20,10 @@
20module Asyncly.Prelude
20module Asyncly.Prelude
22 -- * Construction
22 -- * Construction
24 , fromHandle
27 , fromHandle
26 -- * Elimination
29 -- * Elimination
@@ -74,6 +77,7 @@ import Prelude hiding (filter, drop, dropWhile, take,
74 sum, product, elem, notElem,
77 sum, product, elem, notElem,
75 maximum, minimum, head, last,
78 maximum, minimum, head, last,
80+import qualified Prelude as Prelude
77import qualified System.IO as IO
81import qualified System.IO as IO
79import Asyncly.Core
83import Asyncly.Core
@@ -93,6 +97,12 @@ unfoldr step = fromStream . go
93 Nothing -> stp
97 Nothing -> stp
94 Just (a, b) -> yld a (Just (go b))
98 Just (a, b) -> yld a (Just (go b))
100+-- XXX need eachInterleaved, eachAsync, eachParallel
101+-- | Same as @foldWith (<>)@ but more efficient.
103+each :: (Foldable f, Streaming t) => f a -> t m a
104+each xs = Prelude.foldr cons nil xs
96-- | Read lines from an IO Handle into a stream of Strings.
106-- | Read lines from an IO Handle into a stream of Strings.
97fromHandle :: (MonadIO m, Streaming t) => IO.Handle -> t m String
107fromHandle :: (MonadIO m, Streaming t) => IO.Handle -> t m String
98fromHandle h = fromStream $ go
108fromHandle h = fromStream $ go
@@ -109,6 +119,8 @@ fromHandle h = fromStream $ go
109-- Elimination
119-- Elimination
110------------------------------------------------------------------------------
120------------------------------------------------------------------------------
122+-- Parallel variants of folds?
112-- | Right fold.
124-- | Right fold.
113foldr :: (Monad m, Streaming t) => (a -> b -> b) -> b -> t m a -> m b
125foldr :: (Monad m, Streaming t) => (a -> b -> b) -> b -> t m a -> m b
114foldr step acc m = go (toStream m)
126foldr step acc m = go (toStream m)
@@ -120,6 +132,7 @@ foldr step acc m = go (toStream m)
120 in (runStream m1) Nothing stop yield
132 in (runStream m1) Nothing stop yield
122-- | Right fold with a monadic step function. See 'toList' for an example use.
134-- | Right fold with a monadic step function. See 'toList' for an example use.
135+{-# INLINE foldrM #-}
123foldrM :: Streaming t => (a -> m b -> m b) -> m b -> t m a -> m b
136foldrM :: Streaming t => (a -> m b -> m b) -> m b -> t m a -> m b
124foldrM step acc m = go (toStream m)
137foldrM step acc m = go (toStream m)
@@ -276,7 +289,7 @@ head m =
276 yield a _ = return (Just a)
289 yield a _ = return (Just a)
277 in (runStream (toStream m)) Nothing stop yield
290 in (runStream (toStream m)) Nothing stop yield
279−-- | Extract the first element of the stream, if any.
292+-- | Extract the last element of the stream, if any.
280last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
293last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
281last m = go (toStream m)
294last m = go (toStream m)
@@ -342,6 +355,8 @@ maximum m = go Nothing (toStream m)
342-- Transformation
355-- Transformation
343------------------------------------------------------------------------------
356------------------------------------------------------------------------------
358+-- XXX Parallel variants of these? mapMWith et al. sequenceWith.
345-- | Apply a monadic action to each element of the stream and replace it with
360-- | Apply a monadic action to each element of the stream and replace it with
346-- the result.
361-- the result.
347mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b
362mapM :: (Streaming t, Monad m) => (a -> m b) -> t m a -> t m b