@@ -82,8 +82,10 @@ module Streamly.Prelude
82 -- * Elimination
82 -- * Elimination
83 -- ** General Folds
83 -- ** General Folds
@@ -93,15 +95,27 @@ module Streamly.Prelude
97 , notElem
100 , notElem
101 , maximum
106 , maximum
102 , minimum
107 , minimum
104 , product
109 , product
106 -- ** Map and Fold
120 -- ** Map and Fold
@@ -139,6 +153,9 @@ module Streamly.Prelude
139 -- ** Reordering
153 -- ** Reordering
140 , reverse
154 , reverse
142 -- * Zipping
159 -- * Zipping
143 , zipWith
160 , zipWith
144 , zipWithM
161 , zipWithM
@@ -160,7 +177,7 @@ import Prelude
160 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
177 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
161 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
178 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
162 notElem, maximum, minimum, head, last, tail, length, null,
179 notElem, maximum, minimum, head, last, tail, length, null,
163− reverse, iterate)
180+ reverse, iterate, init, and, or, lookup, foldr1)
164import qualified Prelude
181import qualified Prelude
165import qualified System.IO as IO
182import qualified System.IO as IO
@@ -450,6 +467,12 @@ foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
450-- foldr step acc m = S.foldr step acc $ S.fromStreamK (toStream m)
467-- foldr step acc m = S.foldr step acc $ S.fromStreamK (toStream m)
451foldr f = foldrM (\a b -> return (f a b))
468foldr f = foldrM (\a b -> return (f a b))
470+-- | Right fold, for non-empty streams, using first element as the starting
471+-- value. Returns 'Nothing' if the stream is empty.
472+{-# INLINE foldr1 #-}
473+foldr1 :: Monad m => (a -> a -> a) -> SerialT m a -> m (Maybe a)
453-- | Strict left fold with an extraction function. Like the standard strict
476-- | Strict left fold with an extraction function. Like the standard strict
454-- left fold, but applies a user supplied extraction function (the third
477-- left fold, but applies a user supplied extraction function (the third
455-- argument) to the folded value at the end. This is designed to work with the
478-- argument) to the folded value at the end. This is designed to work with the
@@ -473,6 +496,17 @@ foldl = foldx
473foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
496foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
474foldl' step begin m = S.foldl' step begin $ toStreamS m
497foldl' step begin m = S.foldl' step begin $ toStreamS m
499+-- | Strict left fold, for non-empty streams, using first element as the
500+-- starting value. Returns 'Nothing' if the stream is empty.
501+foldl1' :: Monad m => (a -> a -> a) -> (a -> b) -> SerialT m a -> m (Maybe b)
502+foldl1' step done m = do
505+ Nothing -> return Nothing
507+ res <- foldl' step h t
508+ return $ Just $ done res
476-- XXX replace the recursive "go" with explicit continuations.
510-- XXX replace the recursive "go" with explicit continuations.
477-- | Like 'foldx', but with a monadic step function.
511-- | Like 'foldx', but with a monadic step function.
@@ -517,6 +551,11 @@ head m = K.head m
517tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
551tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
518tail m = K.tail (K.adapt m)
552tail m = K.tail (K.adapt m)
554+-- | Extract all but the last element of the stream, if any.
556+init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
557+init m = K.init (K.adapt m)
520-- | Extract the last element of the stream, if any.
559-- | Extract the last element of the stream, if any.
522-- @since 0.1.1
561-- @since 0.1.1
@@ -559,6 +598,16 @@ all p m = S.all p (toStreamS m)
559any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
598any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
560any p m = S.any p (toStreamS m)
599any p m = S.any p (toStreamS m)
601+-- | Determines if all elements of a boolean stream are True.
603+and :: Monad m => SerialT m Bool -> m Bool
606+-- | Determines wheter at least one element of a boolean stream is True.
608+or :: Monad m => SerialT m Bool -> m Bool
562-- | Determine the sum of all elements of a stream of numbers
611-- | Determine the sum of all elements of a stream of numbers
564-- @since 0.1.0
613-- @since 0.1.0
@@ -587,6 +636,39 @@ minimum m = S.minimum (toStreamS m)
587maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
636maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
588maximum m = S.maximum (toStreamS m)
637maximum m = S.maximum (toStreamS m)
639+-- | Looks the given key up, treating the given stream as an association list.
640+{-# INLINE lookup #-}
641+lookup :: (Monad m, Eq a) => a -> SerialT m (a, b) -> m (Maybe b)
644+-- | Returns the first element of the stream satisfying the given predicate,
647+find :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe a)
650+-- | Finds all the indices of elements satisfying the given predicate.
651+{-# INLINE findIndices #-}
652+findIndices :: IsStream t => (a -> Bool) -> t m a -> t m Int
653+findIndices = K.findIndices
655+-- | Gives the index of the first stream element satisfying the given
657+{-# INLINE findIndex #-}
658+findIndex :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe Int)
659+findIndex p = head . findIndices p
661+-- | Finds the index of all elements in the stream which are equal to the
663+{-# INLINE elemIndices #-}
664+elemIndices :: (IsStream t, Eq a) => a -> t m a -> t m Int
665+elemIndices a = findIndices (==a)
667+-- | Gives the first index of an element in the stream, which equals the given.
668+{-# INLINE elemIndex #-}
669+elemIndex :: (Monad m, Eq a) => a -> SerialT m a -> m (Maybe Int)
670+elemIndex a = findIndex (==a)
590------------------------------------------------------------------------------
672------------------------------------------------------------------------------
591-- Map and Fold
673-- Map and Fold
592------------------------------------------------------------------------------
674------------------------------------------------------------------------------
@@ -812,6 +894,16 @@ reverse m = fromStream $ go K.nil (toStream m)
812 yieldk a r = runIt $ go (a `K.cons` rev) r
894 yieldk a r = runIt $ go (a `K.cons` rev) r
813 in K.unStream rest (rstState st) stop single yieldk
895 in K.unStream rest (rstState st) stop single yieldk
897+------------------------------------------------------------------------------
898+-- Transformation by Inserting
899+------------------------------------------------------------------------------
901+-- | Generate a stream by performing the monadic action inbetween all elements
902+-- of the given stream.
903+{-# INLINE intersperseM #-}
904+intersperseM :: (IsStream t, MonadAsync m) => m a -> t m a -> t m a
905+intersperseM = K.intersperseM
815------------------------------------------------------------------------------
907------------------------------------------------------------------------------
816-- Zipping
908-- Zipping
817------------------------------------------------------------------------------
909------------------------------------------------------------------------------