@@ -90,12 +90,14 @@ module Streamly.Internal.Data.Fold
92 -- ** Terminating Folds
92 -- ** Terminating Folds
93− -- Element folds
93+ -- Element folds. Terminate after inspecting one element. All these can be
94+ -- implemented in terms of the 'maybe' fold.
98− -- Sequence folds
100+ -- Sequence folds. Terminate after inspecting a sequence of elements.
100 -- , lastN
102 -- , lastN
101 -- , (!!)
103 -- , (!!)
@@ -324,7 +326,7 @@ import Prelude hiding
324 , notElem, maximum, minimum, head, last, tail, length, null
326 , notElem, maximum, minimum, head, last, tail, length, null
325 , reverse, iterate, init, and, or, lookup, (!!)
327 , reverse, iterate, init, and, or, lookup, (!!)
326 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
328 , scanl, scanl1, replicate, concatMap, mconcat, foldMap, unzip
327− , span, splitAt, break, mapM, zip)
329+ , span, splitAt, break, mapM, zip, maybe)
328import Streamly.Internal.Data.Fold.Type
330import Streamly.Internal.Data.Fold.Type
@@ -1021,6 +1023,14 @@ genericIndex i = mkFold step (Partial 0) (const Nothing)
1021index :: Monad m => Int -> Fold m a (Maybe a)
1023index :: Monad m => Int -> Fold m a (Maybe a)
1022index = genericIndex
1024index = genericIndex
1026+-- | Map a 'Maybe' returning function on the next element in the stream.
1030+{-# INLINE maybe #-}
1031+maybe :: Monad m => (a -> Maybe b) -> Fold m a (Maybe b)
1032+maybe f = mkFold_ (const (Done . f)) (Partial Nothing)
1024-- Naming notes:
1034-- Naming notes:
1026-- "head" and "next" are two alternative names for the same API. head sounds
1036-- "head" and "next" are two alternative names for the same API. head sounds
@@ -1052,7 +1062,7 @@ index = genericIndex
1053{-# INLINE one #-}
1063{-# INLINE one #-}
1054one :: Monad m => Fold m a (Maybe a)
1064one :: Monad m => Fold m a (Maybe a)
1055−one = mkFold_ (const (Done . Just)) (Partial Nothing)
1065+one = maybe Just
1057-- | Extract the first element of the stream, if any.
1067-- | Extract the first element of the stream, if any.