@@ -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
@@ -175,7 +177,7 @@ import Prelude
175177 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
176178 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
177179 notElem, maximum, minimum, head, last, tail, length, null,
178− reverse, iterate, init, and, or, lookup)
180+ reverse, iterate, init, and, or, lookup, foldr1)
179181import qualified Prelude
180182import qualified System.IO as IO
181183
@@ -465,6 +467,12 @@ foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
465467-- foldr step acc m = S.foldr step acc $ S.fromStreamK (toStream m)
466468foldr f = foldrM (\a b -> return (f a b))
467469
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+
468476-- | Strict left fold with an extraction function. Like the standard strict
469477-- left fold, but applies a user supplied extraction function (the third
470478-- argument) to the folded value at the end. This is designed to work with the
@@ -488,6 +496,17 @@ foldl = foldx
488496foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
489497foldl' step begin m = S.foldl' step begin $ toStreamS m
490498
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+
491510-- XXX replace the recursive "go" with explicit continuations.
492511-- | Like 'foldx', but with a monadic step function.
493512--