So I have some Haskell code in which I’d like to work with Data.Set.
Basically because I didn’t look into alternatives much and need a structure to store elements of Ord without duplicates.
I’ve now come to a situation where I’d like to have something like mapM for Data.Set
so that I can perform monadic operations on a sets individual elements.
I already searched Hayoo for a type like (a -> m b) -> Set a -> m (Set b) but didn’t find anything useful.
I also looked into Data.Traversable just to find out that it has instances for [], Maybe and (Map k) but not for Set.
So my questions are:
- Why is there no mapM for Set in Data.Set?
- Is there already a package that delivers something like mapM that I missed?
- Is it discouraged to want to mapM over Sets? (why and what are alternatives?)
The main problem is that
TraversablerequiresFunctor, and sets cannot be functors since they require anOrdconstraint while functors must be unconstrained.However, sets are foldable so if you don’t need to collect the results, you can use
mapM_fromData.Foldable.If you do need the results, you can go via lists, e.g.