I have this code:
fmapM :: Monad m => (a -> m b) -> (t, a) -> m (t, b)
fmapM f (id, e) = do
ev <- f e
return (id, ev)
which basically applies the function to the 2nd element in the tuple and then "extracts" the monad. Since the tuple is a functor, is there a way to generalize this for all functors? I cannot think of an implementation, but the type signature should be:
fmapM :: (Monad m, Functor f) => (a -> m b) -> f a -> m f b
it would seem like the 2nd step would be a "sequence" operation, which extracts the monad from another functor (the list). But sequence is not generalized to all functors. Can you come up with a generic implementation of fmapM?
Edit: I've realized that an old version of hugs did have this function implemented. However, I can't find the code. Now, it is suggested that I use foldable/traversable to achieve the same.
fmapMyou mean in old Hugs, but that was just a less general version ofTraversable. It's still a class with a different implementation for each type. - shachaffmap-- as well as a lot of other functions -- from justtraverse, but not vice versa.) - shachaf