@@ -82,8 +82,10 @@ module Streamly.Prelude
82 -- * Elimination
82 -- * Elimination
83 -- ** General Folds
83 -- ** General Folds
@@ -175,7 +177,7 @@ import Prelude
175 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
177 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
176 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
178 foldl, map, mapM, mapM_, sequence, all, any, sum, product, elem,
177 notElem, maximum, minimum, head, last, tail, length, null,
179 notElem, maximum, minimum, head, last, tail, length, null,
178− reverse, iterate, init, and, or, lookup)
180+ reverse, iterate, init, and, or, lookup, foldr1)
179import qualified Prelude
181import qualified Prelude
180import qualified System.IO as IO
182import qualified System.IO as IO
@@ -465,6 +467,12 @@ foldr :: Monad m => (a -> b -> b) -> b -> SerialT m a -> m b
465-- 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)
466foldr 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)
468-- | Strict left fold with an extraction function. Like the standard strict
476-- | Strict left fold with an extraction function. Like the standard strict
469-- left fold, but applies a user supplied extraction function (the third
477-- left fold, but applies a user supplied extraction function (the third
470-- 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
@@ -488,6 +496,17 @@ foldl = foldx
488foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
496foldl' :: Monad m => (b -> a -> b) -> b -> SerialT m a -> m b
489foldl' 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
491-- XXX replace the recursive "go" with explicit continuations.
510-- XXX replace the recursive "go" with explicit continuations.
492-- | Like 'foldx', but with a monadic step function.
511-- | Like 'foldx', but with a monadic step function.