I want to create a function that turns Map k [v] (Data.Map.Strict) into Maybe (Map k v).
What it does is this:
Nothing.Just.The only thing I can think of is to do this manually with foldrWithKey' or foldlWithKey'. Are there better ways?
You are looking for traverse :: (Traverseable t, Applicative f) => (a -> f b) -> t a -> f (t b) from Data.Traversable (but also in the Prelude).
justOne :: [a] -> Maybe a
justOne [x] = Just x
justOne _ = Nothing
allJustOne :: Map k [v] -> Maybe (Map k v)
allJustOne = traverse justOne
traverse f is probably best understood as sequenceA . fmap f. sequenceA :: (Traversable t, Applicative f) => t (f a) -> f (t a) on the other hand is a way of pulling the applicative "effects" out of a Traversable structure. For example one might execute a list of IO actions with sequenceA :: [IO a] -> IO [a]. Or in the case of Maybe the result will be Just if all the elements in the Traversable structure are Just and if any of the elements is Nothing the result of sequenceA will also be Nothing.
If you know sequence or mapM, sequenceA and traverse are just generalisations of those.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With