Generalizing the "sequence" for all functors?
I have this code:
fmapM :: Monad m => (a -> mb) -> (t, a) -> m (t, b) fmapM f (id, e) = do ev <- fe return (id, ev) which basically applies the function to the second element in the tuple, and then "extracts" the monad. Since a tuple is a functor, is there a way to generalize it for all functors? I can't think of an implementation, but the type signature should be:
fmapM :: (Monad m, Functor f) => (a -> mb) -> fa -> mfb it would seem that the second step is the operation "sequence", which extracts the monad from another functor (list). But the sequence does not generalize to all functors. Can you come up with a general fmapM implementation?
Edit: I realized that the old version of hugs implemented this function. However, I can not find the code. Now, in order to reach the same level, I use the bend / move function.
The function you are looking for is traverse , from Data.Traversable :
traverse :: (Applicative f, Traversable t) => (a -> fb) -> ta -> f (tb) Note that you cannot traverse any Functor - for example, (r ->) - therefore there is a separate subclass of Traversable functors. Also note that you do not need Monad - just Applicative (this generalization is useful).