Search code examples
haskellfunctor

How to extract keys from Map.Map?


I wish to extract all the keys from a Map but I don't know how to access the keys. I had success grabbing all the values out of any foldable types such as Maps, but I am not sure how to access the keys inside the functor.

getVals :: (Foldable t) => t a -> [a]
getVals x = foldr (:) [] x

How do I define a function like this?

getKeys :: (Map k a) -> [k]

Solution

  • You can't get a Map's keys with its Foldable instance. Instead, use Data.Map.keys.