on master help sign in
Implement findM fold
8dfcadfHarendra Kumar
core/src/Streamly/Internal/Data/Fold.hs+16−9
@@ -103,7 +103,7 @@ module Streamly.Internal.Data.Fold
103 -- , (!!)
103 -- , (!!)
104 , genericIndex
104 , genericIndex
105 , index
105 , index
106− -- , findM
106+ , findM
107 , find
107 , find
108 , lookup
108 , lookup
109 , findIndex
109 , findIndex
@@ -114,7 +114,6 @@ module Streamly.Internal.Data.Fold
114 , any
114 , any
115 , and
115 , and
116 , or
116 , or
117− -- , the
118
117
119 -- * Combinators
118 -- * Combinators
120 -- ** Utilities
119 -- ** Utilities
@@ -1166,17 +1165,25 @@ head = one
1166
1165
1167-- | Returns the first element that satisfies the given predicate.
1166-- | Returns the first element that satisfies the given predicate.
1168--
1167--
1169−-- @since 0.7.0
1168+-- /Pre-release/
1170−{-# INLINE find #-}
1169+{-# INLINE findM #-}
1171−find :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
1170+findM :: Monad m => (a -> m Bool) -> Fold m a (Maybe a)
1172−find predicate = foldt' step (Partial ()) (const Nothing)
1171+findM predicate = Fold step (return $ Partial ()) (const $ return Nothing)
1173
1172
1174 where
1173 where
1175
1174
1176 step () a =
1175 step () a =
1177− if predicate a
1176+ (\r ->
1177+ if r
1178− then Done (Just a)
1178+ then Done (Just a)
1179− else Partial ()
1179+ else Partial ()) <$> predicate a
1180+
1181+-- | Returns the first element that satisfies the given predicate.
1182+--
1183+-- @since 0.7.0
1184+{-# INLINE find #-}
1185+find :: Monad m => (a -> Bool) -> Fold m a (Maybe a)
1186+find p = findM (return . p)
1180
1187
1181-- | In a stream of (key-value) pairs @(a, b)@, return the value @b@ of the
1188-- | In a stream of (key-value) pairs @(a, b)@, return the value @b@ of the
1182-- first pair where the key equals the given value @a@.
1189-- first pair where the key equals the given value @a@.