The
findMOf function takes a
Lens (or
Getter,
Iso,
Fold, or
Traversal), a monadic predicate and
a structure and returns in the monad the leftmost element of the
structure matching the predicate, or
Nothing if there is no
such element.
>>> findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,4,6)
"Checking 1"
"Checking 3"
"Checking 4"
Just 4
>>> findMOf each ( \x -> print ("Checking " ++ show x) >> return (even x)) (1,3,5,7)
"Checking 1"
"Checking 3"
"Checking 5"
"Checking 7"
Nothing
findMOf :: (Monad m, Getter s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Fold s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Iso' s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Lens' s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf :: (Monad m, Traversal' s a) -> (a -> m Bool) -> s -> m (Maybe a)
findMOf folded :: (Monad m, Foldable f) => (a -> m Bool) -> f a -> m (Maybe a)
ifindMOf l ≡ findMOf l . Indexed
A simpler version that didn't permit indexing, would be:
findMOf :: Monad m => Getting (Endo (m (Maybe a))) s a -> (a -> m Bool) -> s -> m (Maybe a)
findMOf l p = foldrOf l (a y -> p a >>= x -> if x then return (Just a) else y) $ return Nothing