I’m basically looking for the find analogue of filterM:
findM :: (a -> m Bool) -> [a] -> m (Maybe a)
but I haven’t been able to write it myself using find and some sort of lifting function. I’m currently doing it via Data.Maybe.listToMaybe:
x <- filterM f list
return $ listToMaybe x
which works, but it seems like I should be able to do it directly with find.
Edit: Found some stuff on hackage: Control.Monad.Loops.firstM and Control.Monad.TM.findM both do what I want
Well, you could write it “from scratch” like
though I realize that is much less elegant than what you are asking for. I could not figure out how to do it with some sort of lifting function. I feel like the lifting would have to occur at the recursive step of
find, which is hidden inside the function and not available to be lifted. Though I would love to see someone prove me wrong.