Skip to content

Commit

Permalink
Add toMap and fromMap to Data.Set.
Browse files Browse the repository at this point in the history
  • Loading branch information
np authored and milesfrain committed Dec 23, 2020
1 parent 4772100 commit 5c3e0a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"purescript-st": "master",
"purescript-tailrec": "master",
"purescript-tuples": "master",
"purescript-unfoldable": "master",
"purescript-unsafe-coerce": "master"
"purescript-unfoldable": "master"
},
"devDependencies": {
"purescript-quickcheck": "master",
Expand Down
7 changes: 4 additions & 3 deletions src/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ module Data.Map
import Prelude

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)
import Data.Set (Set)
import Unsafe.Coerce (unsafeCoerce)
import Data.Set (Set, fromMap)

-- | The set of keys of the given map.
-- | See also `Data.Set.fromMap`.
keys :: forall k v. Map k v -> Set k
keys = (unsafeCoerce :: Map k Unit -> Set k) <<< void
keys = fromMap <<< void
11 changes: 11 additions & 0 deletions src/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module Data.Set
, filter
, mapMaybe
, catMaybes
, toMap
, fromMap
) where

import Prelude hiding (map)
Expand Down Expand Up @@ -195,3 +197,12 @@ mapMaybe f = foldr (\a acc -> maybe acc (\b -> insert b acc) (f a)) empty
-- | Filter a set of optional values, discarding values that contain `Nothing`
catMaybes :: forall a. Ord a => Set (Maybe a) -> Set a
catMaybes = mapMaybe identity

-- | A set is a map with no value attached to each key.
toMap :: forall a. Set a -> M.Map a Unit
toMap (Set s) = s

-- | A map with no value attached to each key is a set.
-- | See also `Data.Map.keys`.
fromMap :: forall a. M.Map a Unit -> Set a
fromMap = Set

0 comments on commit 5c3e0a6

Please sign in to comment.