
Can someone give me a clever way to implement mapKeysMaybe, defined as follows? Ord k => (k -> Maybe k) -> Map k a -> Map k a where the transformed keys that evaluate to Just are the only ones kept? I am thinking this will probably help me understand a new type, like traversable or something.

Well, this might not be the most efficient, but:
mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)
What do you reckon?
On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle
Can someone give me a clever way to implement mapKeysMaybe, defined as follows?
Ord k => (k -> Maybe k) -> Map k a -> Map k a
where the transformed keys that evaluate to Just are the only ones kept?
I am thinking this will probably help me understand a new type, like traversable or something.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Oops disregard that. The mapMaybeWithKey doesn't quite do what I expected.
On Fri, Nov 6, 2015 at 6:43 PM, Lyndon Maydwell
Well, this might not be the most efficient, but:
mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)
What do you reckon?
On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle
wrote: Can someone give me a clever way to implement mapKeysMaybe, defined as follows?
Ord k => (k -> Maybe k) -> Map k a -> Map k a
where the transformed keys that evaluate to Just are the only ones kept?
I am thinking this will probably help me understand a new type, like traversable or something.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Here's a more correct solution, unfortunately it's not as nice.
{-# LANGUAGE TupleSections #-}
import Data.Map hiding (mapMaybe)
import Data.Maybe
mkm :: Ord k => (t -> Maybe k) -> Map t a -> Map k a
mkm f = fromList . mapMaybe (\(k,v) -> fmap (,v) (f k)) . toList
On Fri, Nov 6, 2015 at 6:46 PM, Lyndon Maydwell
Oops disregard that. The mapMaybeWithKey doesn't quite do what I expected.
On Fri, Nov 6, 2015 at 6:43 PM, Lyndon Maydwell
wrote: Well, this might not be the most efficient, but:
mapKeysMaybe f = Data.Map.mapMaybeWithKey (const . f)
What do you reckon?
On Fri, Nov 6, 2015 at 6:29 PM, Dennis Raddle
wrote: Can someone give me a clever way to implement mapKeysMaybe, defined as follows?
Ord k => (k -> Maybe k) -> Map k a -> Map k a
where the transformed keys that evaluate to Just are the only ones kept?
I am thinking this will probably help me understand a new type, like traversable or something.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Dennis Raddle
-
Lyndon Maydwell