Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toMap and fromMap to Data.Set. - Retry #31

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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