mirror of
https://github.com/composewell/streamly.git
synced 2024-09-11 11:37:22 +03:00
Add findIndices fold
This commit is contained in:
parent
20916e7427
commit
ea1b48fbbb
@ -128,9 +128,9 @@ module Streamly.Internal.Data.Fold
|
||||
, mapMaybe
|
||||
-- , mapMaybeM
|
||||
|
||||
{-
|
||||
-- ** Scanning Filters
|
||||
, findIndices
|
||||
{-
|
||||
, elemIndices
|
||||
|
||||
-- ** Insertion
|
||||
@ -893,6 +893,23 @@ findIndex predicate = mkFold step (Partial 0) (const Nothing)
|
||||
then Done $ Just i
|
||||
else Partial (i + 1)
|
||||
|
||||
-- XXX If we have a Continue contructor we won't need a Maybe type for the
|
||||
-- scan. The fold will have the ability to skip producing output.
|
||||
--
|
||||
-- | Returns all indices that satisfy the given predicate.
|
||||
--
|
||||
{-# INLINE findIndices #-}
|
||||
findIndices :: Monad m => (a -> Bool) -> Fold m a (Maybe Int)
|
||||
findIndices predicate =
|
||||
fmap (either (const Nothing) Just) $ foldl' step (Left (-1))
|
||||
|
||||
where
|
||||
|
||||
step i a =
|
||||
if predicate a
|
||||
then Right (either id id i + 1)
|
||||
else Left (either id id i + 1)
|
||||
|
||||
-- | Returns the first index where a given value is found in the stream.
|
||||
--
|
||||
-- > elemIndex a = Fold.findIndex (== a)
|
||||
|
Loading…
Reference in New Issue
Block a user