I have a list of maybes and a function that gives me a node's color (if present):
maybeNeighbors :: [Maybe Node]
nodeColor :: Node -> Maybe Color
Now I'd like to map colors to nodes, and as intermediate step I want to have a list of tuples:
coloredList :: [(Color, [Node])]
(Because I'll construct a Map from it later with listToUFM_C (++) listColored)
Here is what I have so far, it works but seems ugly:
listColored = mapMaybe (\n -> nodeColor n >>= \c -> Just (c, [n])) $ catMaybes maybeNeighbors
(using catMaybes and mapMaybe from Data.Maybe)
I feel like I'm missing something, that I should be able to do something like (fmap . fmap) func maybeNeighbors, but I can't figure out how func should look like.
Or a function like this, which I can't find either: (Maybe a -> Maybe b) -> [Maybe a] -> [Maybe b]
Edit:
I'm working on a graph coloring problem and I want a list of nodes that have the same color. Here is an example to test in GHCi:
let l = [Just (1, Just 'a'), Just (2, Just 'a'), Nothing, Just (3, Just 'b'), Just (4, Nothing)]