Created: 2022-02-06 22:30

Is there some version of Alternative’s <|> that does this:

(Alternative f, Monad m) => m (f a) -> m (f a) -> m (f a)

This type checks liftM2 (<|>)

But the problem is it will run both actions, but I only want to run the 2nd one if the first one didn’t succeed:

Here’s liftM2 implementation in Base:

liftM2  :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) }

In practice what I’m trying to do is make a request (the m), if it doesn’t succeed (f a is Maybe a) then I run the second request.

There’s Alternative for MaybeT, that’s probably what I want.

#continuehere try Alternative for MaybeT / ExceptT