Skip to content
Discussions/App Development/Why does DA.Next.Set not have a map function?Forum ↗

Why does DA.Next.Set not have a map function?

App Development7 posts391 views6 likesLast activity May 2020
GE
georgOP
Apr 2020

Or have I just not found it yet?

AN
anthony
May 2020

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.

GE
georg
May 2020

Intuitively, I would’ve considered sets to be an unordered collection. By what are they ordered?

AN
anthony
May 2020

Nevermind, my answer was incorrect.

LE
Leonid_Shlyapnikov
May 2020

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:

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...

CO
cocreature
May 2020

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.

LU
Luciano
May 2020
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.

← Back to Discussions