@@ -82,8 +82,10 @@ module Streamly.Prelude
8282 -- * Elimination
8383 -- ** General Folds
8484 , foldr
85+ , foldr1
8586 , foldrM
8687 , foldl'
88+ , foldl1'
8789 , foldlM'
8890 , foldx
8991 , foldxM
@@ -93,15 +95,27 @@ module Streamly.Prelude
9395 , head
9496 , tail
9597 , last
98+ , init
9699 , elem
97100 , notElem
98101 , length
99102 , all
100103 , any
104+ , and
105+ , or
101106 , maximum
102107 , minimum
103108 , sum
104109 , product
110+ , lookup
111+ , find
112+
113+ -- *** Indices
114+ , findIndices
115+ , findIndex
116+ , elemIndices
117+ , elemIndex
118+
105119
106120 -- ** Map and Fold
107121 , mapM_
@@ -139,6 +153,9 @@ module Streamly.Prelude
139153 -- ** Reordering
140154 , reverse
141155
156+ -- ** Inserting
157+ , intersperseM
158+
142159 -- * Zipping
143160 , zipWith
144161 , zipWithM
@@ -160,7 +177,7 @@ import Prelude
160177 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
161178 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
162179 notElem, maximum, minimum, head, last, tail, length, null,
163− reverse, iterate)
180+ reverse, iterate, init, and, or, lookup, foldr1)
164181import qualified Prelude
165182import qualified System.IO as IO
166183
@@ -450,6 +467,12 @@ foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
450467-- foldr step acc m = S.foldr step acc $ S.fromStreamK (toStream m)
451468foldr f = foldrM (\a b -> return (f a b))
452469
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)
474+foldr1 = K.foldr1
475+
453476-- | Strict left fold with an extraction function. Like the standard strict
454477-- left fold, but applies a user supplied extraction function (the third
455478-- argument) to the folded value at the end. This is designed to work with the
@@ -473,6 +496,17 @@ foldl = foldx
473496foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
474497foldl' step begin m = S.foldl' step begin $ toStreamS m
475498
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
503+ r <- uncons m
504+ case r of
505+ Nothing -> return Nothing
506+ Just (h, t) -> do
507+ res <- foldl' step h t
508+ return $ Just $ done res
509+
476510-- XXX replace the recursive "go" with explicit continuations.
477511-- | Like 'foldx', but with a monadic step function.
478512--
@@ -517,6 +551,11 @@ head m = K.head m
517551tail :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
518552tail m = K.tail (K.adapt m)
519553
554+-- | Extract all but the last element of the stream, if any.
555+{-# INLINE init #-}
556+init :: (IsStream t, Monad m) => SerialT m a -> m (Maybe (t m a))
557+init m = K.init (K.adapt m)
558+
520559-- | Extract the last element of the stream, if any.
521560--
522561-- @since 0.1.1
@@ -559,6 +598,16 @@ all p m = S.all p (toStreamS m)
559598any :: Monad m => (a -> Bool) -> SerialT m a -> m Bool
560599any p m = S.any p (toStreamS m)
561600
601+-- | Determines if all elements of a boolean stream are True.
602+{-# INLINE and #-}
603+and :: Monad m => SerialT m Bool -> m Bool
604+and = all (==True)
605+
606+-- | Determines wheter at least one element of a boolean stream is True.
607+{-# INLINE or #-}
608+or :: Monad m => SerialT m Bool -> m Bool
609+or = any (==True)
610+
562611-- | Determine the sum of all elements of a stream of numbers
563612--
564613-- @since 0.1.0
@@ -587,6 +636,39 @@ minimum m = S.minimum (toStreamS m)
587636maximum :: (Monad m, Ord a) => SerialT m a -> m (Maybe a)
588637maximum m = S.maximum (toStreamS m)
589638
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)
642+lookup = K.lookup
643+
644+-- | Returns the first element of the stream satisfying the given predicate,
645+-- if any.
646+{-# INLINE find #-}
647+find :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe a)
648+find = K.find
649+
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
654+
655+-- | Gives the index of the first stream element satisfying the given
656+-- preficate.
657+{-# INLINE findIndex #-}
658+findIndex :: Monad m => (a -> Bool) -> SerialT m a -> m (Maybe Int)
659+findIndex p = head . findIndices p
660+
661+-- | Finds the index of all elements in the stream which are equal to the
662+-- given.
663+{-# INLINE elemIndices #-}
664+elemIndices :: (IsStream t, Eq a) => a -> t m a -> t m Int
665+elemIndices a = findIndices (==a)
666+
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)
671+
590672------------------------------------------------------------------------------
591673-- Map and Fold
592674------------------------------------------------------------------------------
@@ -812,6 +894,16 @@ reverse m = fromStream $ go K.nil (toStream m)
812894 yieldk a r = runIt $ go (a `K.cons` rev) r
813895 in K.unStream rest (rstState st) stop single yieldk
814896
897+------------------------------------------------------------------------------
898+-- Transformation by Inserting
899+------------------------------------------------------------------------------
900+
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
906+
815907------------------------------------------------------------------------------
816908-- Zipping
817909------------------------------------------------------------------------------