The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with
the type signature
lookup :: Ord k => k -> Map k a -> Maybe a
It used to have the more general type of
lookup :: (Monad m, Ord k) => k -> Map k a -> m a
I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I have to use an older version of the library?
Don’s
liftconvertsMaybe‘s elements to their generalMonadcounterparts, so perhaps it should be namedconvertorgeneralizeor something;-)If you just want to use
lookupmainly in list comprehensions and other monads that implement afail, you could also make use of the mapping of pattern match failure tofail:Prelude> [ v | Just v <- return $ lookup "hi" [("ho","silver")] ] [] Prelude> [ v | Just v <- return $ lookup "ho" [("ho","silver")] ] ["silver"] Prelude> do Just v <- return $ lookup "hi" [("ho","silver")] ; print v *** Exception: user error (Pattern match failure in do expression at <interactive>:1:3-8) Prelude> do Just v <- return $ lookup "ho" [("ho","silver")] ; print v "silver"