On Tue, May 14, 2013 at 4:21 PM, Adrian May <adrian.alexander.may@gmail.com> wrote:
I have a really annoying scrap of code:

unmaybe Nothing = mempty
unmaybe (Just dia) = dia

It happened because I'm using Diagrams but building my diagram requires looking something up in a list using findIndex, which returns Maybe Int.

What instance of Monoid is this? Because Int has both a Sum Int and a Product Int instance so you can't just apply unmaybe to (Just 3 :: Maybe Int).

Defining unmaybe Nothing = 0 prompts the question: how will you distinguish misses versus hits on the head of the list? Presumably you don't want to.

You might be interested in the totalized lookup functions defined in my private toolkit (hayoo returns nothing):

-- tlookup :: (Eq a) => b -> a -> [(a, b)] -> b
tlookup b a abs = fromMaybe b $ lookup a abs
tlookup0  a abs = tlookup mempty a abs

-- Kim-Ee