@@ -29,10 +29,23 @@ module Asyncly.Prelude
34− -- * Special folds
33+ -- * Elimination Special Folds
38 , takeWhile
51 , takeWhile
@@ -57,7 +70,10 @@ import Control.Monad.IO.Class (MonadIO(..))
57import Data.Semigroup (Semigroup(..))
70import Data.Semigroup (Semigroup(..))
58import Prelude hiding (filter, drop, dropWhile, take,
71import Prelude hiding (filter, drop, dropWhile, take,
59 takeWhile, zipWith, foldr, foldl,
72 takeWhile, zipWith, foldr, foldl,
60− mapM, mapM_, sequence)
73+ mapM, mapM_, sequence, all, any,
74+ sum, product, elem, notElem,
75+ maximum, minimum, head, last,
61import qualified System.IO as IO
77import qualified System.IO as IO
63import Asyncly.Core
79import Asyncly.Core
@@ -223,6 +239,105 @@ dropWhile p m = fromStream $ go (toStream m)
223 | otherwise = yld a (Just x)
239 | otherwise = yld a (Just x)
224 in (runStream m1) ctx stp yield
240 in (runStream m1) ctx stp yield
242+-- | Determine whether all elements of a stream satisfy a predicate.
243+all :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
244+all p m = go (toStream m)
247+ let yield a Nothing | p a = return True
248+ | otherwise = return False
249+ yield a (Just x) | p a = go x
250+ | otherwise = return False
251+ in (runStream m1) Nothing (return True) yield
253+-- | Determine whether any of the elements of a stream satisfy a predicate.
254+any :: (Streaming t, Monad m) => (a -> Bool) -> t m a -> m Bool
255+any p m = go (toStream m)
258+ let yield a Nothing | p a = return True
259+ | otherwise = return False
260+ yield a (Just x) | p a = return True
262+ in (runStream m1) Nothing (return False) yield
264+-- | Determine the sum of all elements of a stream of numbers
265+sum :: (Streaming t, Monad m, Num a) => t m a -> m a
268+-- | Determine the product of all elements of a stream of numbers
269+product :: (Streaming t, Monad m, Num a) => t m a -> m a
270+product = foldl (*) 0 id
272+-- | Extract the first element of the stream, if any.
273+head :: (Streaming t, Monad m) => t m a -> m (Maybe a)
275+ let stop = return Nothing
276+ yield a _ = return (Just a)
277+ in (runStream (toStream m)) Nothing stop yield
279+-- | Extract the first element of the stream, if any.
280+last :: (Streaming t, Monad m) => t m a -> m (Maybe a)
281+last m = go (toStream m)
284+ let stop = return Nothing
285+ yield a Nothing = return (Just a)
286+ yield _ (Just x) = go x
287+ in (runStream m1) Nothing stop yield
289+-- | Determine whether an element is present in the stream.
290+elem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
291+elem e m = go (toStream m)
294+ let stop = return False
295+ yield a Nothing = return (a == e)
296+ yield a (Just x) = if (a == e) then return True else go x
297+ in (runStream m1) Nothing stop yield
299+-- | Determine whether an element is not present in the stream.
300+notElem :: (Streaming t, Monad m, Eq a) => a -> t m a -> m Bool
301+notElem e m = go (toStream m)
304+ let stop = return True
305+ yield a Nothing = return (a /= e)
306+ yield a (Just x) = if (a == e) then return False else go x
307+ in (runStream m1) Nothing stop yield
309+-- | Determine the length of the stream.
310+length :: (Streaming t, Monad m) => t m a -> m Int
311+length = foldl (\n _ -> n + 1) 0 id
313+-- | Determine the minimum element in a stream.
314+minimum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
315+minimum m = go Nothing (toStream m)
319+ yield a Nothing = return $ min_ a r
320+ yield a (Just x) = go (min_ a r) x
321+ in (runStream m1) Nothing stop yield
323+ min_ a r = case r of
325+ Just e -> Just $ min a e
327+-- | Determine the maximum element in a stream.
328+maximum :: (Streaming t, Monad m, Ord a) => t m a -> m (Maybe a)
329+maximum m = go Nothing (toStream m)
333+ yield a Nothing = return $ max_ a r
334+ yield a (Just x) = go (max_ a r) x
335+ in (runStream m1) Nothing stop yield
337+ max_ a r = case r of
339+ Just e -> Just $ max a e
226------------------------------------------------------------------------------
341------------------------------------------------------------------------------
227-- Transformation
342-- Transformation
228------------------------------------------------------------------------------
343------------------------------------------------------------------------------