Julian Ospald has opened a pull request[*] for containers to add a traversal operation to Data.Set. Julian specifically requested

forM :: (Ord b, Monad m) => Set a -> (a -> m b) -> m (Set b)

Personally, I think it would be better to offer the unflipped versions:

-- The most general version

traverse :: (Ord b, Applicative f) => (a -> f b) -> Set a -> f (Set b)

traverse f = fmap fromList . traverse f . toList

-- A more efficient, strictly accumulating version for "strict"

-- monads like IO, strict State, etc.

mapM :: (Ord b, Monad m) => (a -> m b) -> Set a -> m (Set b)

mapM f s0 = foldr go return s0 empty

where

go x r s = f x >>= \y -> r $! insert y s

The latter can also be written, perhaps less efficiently, as

mapM :: (Ord b, Monad m) => (a -> m b) -> Set a -> m (Set b)

mapM f = foldM go empty

where

go s x = f x >>= \y -> pure $! insert y s

As usual, the main question is what names are appropriate. Typically, containers assumes that users import its modules qualified, and therefore we should just use `traverse` and `mapM`. But clashing with well-known and widely-used names often causes breakage anyway, so I figured I should bring the matter to the libraries list.

Thanks,

David Feuer