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

**URL:** <https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311>\
**Category:** App Development\
**Created:** [April 30, 2020, 9:52pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311 "2020-04-30T21:52:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [April 30, 2020, 9:52pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/1 "2020-04-30T21:52:07Z")

</div>

Or have I just not found it yet?

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [May 1, 2020, 2:38pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/2 "2020-05-01T14:38:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![georg](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/georg/32/368_2.png) [@georg](https://forum.canton.network/u/georg)\
**Post date:** [May 1, 2020, 2:47pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/3 "2020-05-01T14:47:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anthony](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/anthony/32/419_2.png) [@anthony](https://forum.canton.network/u/anthony)\
**Post date:** [May 1, 2020, 3:09pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/4 "2020-05-01T15:09:24Z")

</div>

Nevermind, my answer was incorrect.

---

<div class="post-metadata">

**Author:** ![Leonid\_Shlyapnikov](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/leonid_shlyapnikov/32/241_2.png) [@Leonid\_Shlyapnikov](https://forum.canton.network/u/Leonid_Shlyapnikov)\
**Post date:** [May 1, 2020, 5:36pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/5 "2020-05-01T17:36:36Z")

</div>

There is `fmap` [https://docs.daml.com/daml/reference/base.html#function-ghc-base-fmap-41707](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:

> **[Why Data.Set (containers) doesn't have Functor and Monad instance?](https://discourse.haskell.org/t/why-data-set-containers-doesnt-have-functor-and-monad-instance/832/2)**
>
> 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...

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 3, 2020, 8:01am UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/6 "2020-05-03T08:01:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Luciano](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/luciano/32/319_2.png) [@Luciano](https://forum.canton.network/u/Luciano)\
**Post date:** [May 5, 2020, 1:04pm UTC](https://forum.canton.network/t/why-does-da-next-set-not-have-a-map-function/311/7 "2020-05-05T13:04:58Z")

</div>

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