purescript-deprecated/purescript-sets

Add map?

Closed this issue · 3 comments

I realise a Functor instance is not possible, but would a monomorphic map function be ok?

Data.Set.map :: forall a b. (a -> b) -> Set a -> Set b

I think Data.Set.map should have an Ord constraint map :: forall a b. (Ord b) => (a -> b) -> Set a -> Set b and would basically be fromList <<< map f <<< toList.

Of course we also may add a monotonic version like Data.Set.mapMonotonic. It should be possible to make it O(n) but I would prefer such things to be in Data.Set.Unsafe.

I think both map and Unsafe.mapMonotonic could be useful 👍

👍

Set.map could actually be a version of fromFoldable which applies a function before inserting. Given that we have Foldable Set.

fromFoldable :: forall f a. (Foldable f, Ord a) => f a -> Set a
map :: forall f a b. (Foldable f, Ord b) => (a -> b) -> f a -> Set b

Alternatively, rename that to fromFoldableBy and then have an alias as map which is specific to Set.

fromFoldableBy :: forall f a b. (Foldable f, Ord b) => (a -> b) -> f a -> Set b
fromFoldableBy f = foldl (\m a -> insert (f a) m) empty

map :: forall f a b. Ord b => (a -> b) -> Set a -> Set b
map = fromFoldableBy