Or have I just not found it yet?
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 with toList
first and then map over that.
Intuitively, I would’ve considered sets to be an unordered collection. By what are they ordered?
Nevermind, my answer was incorrect.
There is fmap
https://docs.daml.com/daml/reference/base.html#function-ghc-base-fmap-41707
However DAML stdlib does not have a Functor
implemented for DA.Next.Set
and this must be after Haskell’s Data.Set
type which also does not implement Functor
. Quick googling:
There are a few different questions here. Let me first answer the original question:
Is there a good reason why DA.Next.Set
does not have a map
function?
No and imho we should add one.
Is DA.Next.Set ordered?
DA.Next.Set
is a wrapper around TextMap ()
so we have to look at that.
There are two parts to this: First, toList
implies an order on the keys. This is guaranteed to be the lexicographic order on Text
values. Second, the internal implementation of TextMap
is a scala HashMap
which 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 f
as fromList . 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 implement map
either way and as mentioned above HashMap
isn’t sorted anyway.
Can we implement a Functor
instance for Set
?
No, to insert something into a Set
you need a MapKey
constraint. However, not all types can have instances of MapKey
, in particular, functions are not instances of MapKey
. Functor
requires you to implement fmap : (a -> b) -> Set a -> Set b
but here we can only get map : (MapKey a, MapKey b) -> (a -> b) -> Set a -> Set b
.
Can someone give a counter-example for composition? I’m probably being thick but I can’t think of an example off-hand.