Why does DA.Next.Set not have a map function?
- georgOP 1
Or have I just not found it yet?
- anthony 1
Essentially Sets are ordered so you can’t map over them because there’s no guarantee the mapping doesn’t change the order.You’d probably want to transform your Set into a list withtoListfirst and then map over that. - georg 1
Intuitively, I would’ve considered sets to be an unordered collection. By what are they ordered?
- anthony
Nevermind, my answer was incorrect.
- Leonid_Shlyapnikov 1
There is
fmaphttps://docs.daml.com/daml/reference/base.html#function-ghc-base-fmap-41707However DAML stdlib does not have a
Functorimplemented forDA.Next.Setand this must be after Haskell’sData.Settype which also does not implementFunctor. Quick googling:
Haskell Community – 7 Aug 19
Why Data.Set (containers) doesn't have Functor and Monad instance?
For efficiency reasons, Set has an Ord constraint, but Functor has not! On top of that, even if the Ord constraint were not a problem, Set would still not satisfy fmap (f . g) == fmap f . fmap g for some peculiar Eq instances. E.g.: import...
- cocreature 2
There are a few different questions here. Let me first answer the original question:
Is there a good reason why
DA.Next.Setdoes not have amapfunction?No and imho we should add one.
Is DA.Next.Set ordered?
DA.Next.Setis a wrapper aroundTextMap ()so we have to look at that.
There are two parts to this: First,toListimplies an order on the keys. This is guaranteed to be the lexicographic order onTextvalues. Second, the internal implementation ofTextMapis a scalaHashMapwhich is not ordered so the sorting happens when converting to a list.Does it matter if the internal implementation is sorted or not?
Not really, you can always implement
map fasfromList . map f . toList. If your set is sorted and you know that mapping preserves the order you can be a bit more efficient but you can implementmapeither way and as mentioned aboveHashMapisn’t sorted anyway.Can we implement a
Functorinstance forSet?No, to insert something into a
Setyou need aMapKeyconstraint. However, not all types can have instances ofMapKey, in particular, functions are not instances ofMapKey.Functorrequires you to implementfmap : (a -> b) -> Set a -> Set bbut here we can only getmap : (MapKey a, MapKey b) -> (a -> b) -> Set a -> Set b. - Luciano
Leonid_Shlyapnikov:Set would still not satisfy fmap (f . g) == fmap f .
Can someone give a counter-example for composition? I’m probably being thick but I can’t think of an example off-hand.