I would like to define a typeclass with a single function lookup
, in the hopes to use it across multiple container types that have a key → value mapping.
Example implementations
- For Map
lookup = Data.Map.lookup
- For list of (k,v) tuples
lookup = Data.List.lookup
I’m stuck on how to unpack the type of a list of tuples to fit a definition.
class Lookup c k where
lookup :: k -> c k a -> Maybe a
instance (Ord k) => Lookup Map k where
lookup :: k -> Map k a -> Maybe a
lookup = Data.Map.lookup
instance (Eq k) => Lookup ??? k where
lookup :: k -> [] (k,a) -> Maybe a -- [] (k,a) for visual symmetry
lookup = Data.List.lookup