Suppose I have two functions
f :: Monad m => a -> m a
g :: a -> a
that I want to consecutively apply to some element like so:
(return x) >>= f >>= g
This doesn't work because g is pure, so I first need to "artificially" turn it monadic. One possibility is
(return x) >>= f >>= (return . g)
which isn't very intuitive to me. Another possibility is to use that a Monad is Applicative:
(return g) <*> ((return x) >>= f)
But this is not very intuitive because of the different order of function and argument:
(>>=) :: Monad m => m a -> (a -> m b) -> m b
(<*>) :: Applicative f => f (a -> b) -> f a -> f b
What is the canonical way to deal with this? If (>>==) = (flip ((<*>) . pure))
one can write ((pure x) >>= f) >>== g
, which would be fine except for operator precedence. Of course pure functions in monadic code are a common thing so surely there is a standard way to deal with them?
Edit: I didn't say this originally but I was thinking of a situation where I had several functions, some pure, some monadic, and I wanted to apply them in some random order.