Say I have function two functions f
and g
that both take in regular values and return an Either
value like so:
g :: a -> Either x b
f :: b -> Either x c
How do I chain the two together to get something like f . g
?
The best solution I have come up with is creating a helper function called applyToRight
that works like this
applyToRight :: (a -> Either x b) -> Either x a -> Either x b
applyToRight f x =
case x of
Left a -> Left a
Right b -> f b
So that I can then do
applyToRight f (g a)
In this case I am specifically talking about Either
, but I think this problem could be generalized to all applicative functors. What is the most elegant way to deal with this?