Given a function signature like:
thing :: Maybe (a -> b) -> a -> Maybe b
How can I apply the (a -> b)
function to a
without having to unwrap (to use the Rust term) and re-wrap it? I’ve come up with these solutions, which I believe are functionally identical, but it feels like there’s probably a cleaner way to do it without having to use Just
/return
/pure
, etc., as well as propagating a Nothing
value through it. This is certainly not a showstopper for me, I’m just trying to become more familiar and comfortable with the tools at my disposal for dealing with monads/applicative functors.
thing :: Maybe (a -> b) -> a -> Maybe b
thing (Just op) = Just . op
thing1 :: Maybe (a -> b) -> a -> Maybe b
thing1 maybeOp y = maybeOp >>= \op -> Just $ op y
thing2 :: Maybe (a -> b) -> a -> Maybe b
thing2 maybeOp = ap maybeOp . Just