Skip to content

Commit d004644

Browse files
milesfrainnp
andauthored
Add toMap and fromMap to Data.Set (#31)
Co-authored-by: Nicolas Pouillard <[email protected]>
1 parent 4772100 commit d004644

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
"purescript-st": "master",
2727
"purescript-tailrec": "master",
2828
"purescript-tuples": "master",
29-
"purescript-unfoldable": "master",
30-
"purescript-unsafe-coerce": "master"
29+
"purescript-unfoldable": "master"
3130
},
3231
"devDependencies": {
3332
"purescript-quickcheck": "master",

src/Data/Map.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ module Data.Map
66
import Prelude
77

88
import Data.Map.Internal (Map, alter, catMaybes, checkValid, delete, empty, filter, filterKeys, filterWithKey, findMax, findMin, foldSubmap, fromFoldable, fromFoldableWith, fromFoldableWithIndex, insert, insertWith, isEmpty, isSubmap, lookup, lookupGE, lookupGT, lookupLE, lookupLT, member, pop, showTree, singleton, size, submap, toUnfoldable, toUnfoldableUnordered, union, unionWith, unions, intersection, intersectionWith, difference, update, values, mapMaybeWithKey, mapMaybe)
9-
import Data.Set (Set)
10-
import Unsafe.Coerce (unsafeCoerce)
9+
import Data.Set (Set, fromMap)
1110

11+
-- | The set of keys of the given map.
12+
-- | See also `Data.Set.fromMap`.
1213
keys :: forall k v. Map k v -> Set k
13-
keys = (unsafeCoerce :: Map k Unit -> Set k) <<< void
14+
keys = fromMap <<< void

src/Data/Set.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module Data.Set
2727
, filter
2828
, mapMaybe
2929
, catMaybes
30+
, toMap
31+
, fromMap
3032
) where
3133

3234
import Prelude hiding (map)
@@ -195,3 +197,12 @@ mapMaybe f = foldr (\a acc -> maybe acc (\b -> insert b acc) (f a)) empty
195197
-- | Filter a set of optional values, discarding values that contain `Nothing`
196198
catMaybes :: forall a. Ord a => Set (Maybe a) -> Set a
197199
catMaybes = mapMaybe identity
200+
201+
-- | A set is a map with no value attached to each key.
202+
toMap :: forall a. Set a -> M.Map a Unit
203+
toMap (Set s) = s
204+
205+
-- | A map with no value attached to each key is a set.
206+
-- | See also `Data.Map.keys`.
207+
fromMap :: forall a. M.Map a Unit -> Set a
208+
fromMap = Set

0 commit comments

Comments
 (0)