Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Implements keysSet #48

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions src/Data/Set.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Data.Set
, subset
, properSubset
, intersection
, keysSet
) where

import Prelude hiding (map)
Expand Down Expand Up @@ -176,3 +177,9 @@ intersection s1 s2 = fromFoldable $ runPure (runSTArray (emptySTArray >>= inters
LT -> pure $ Loop {a: l + 1, b: r}
GT -> pure $ Loop {a: l, b: r + 1}
else pure $ Done acc

-- | Extracts the keys of a `Map` as a `Set`.
-- |
-- | Running time: `O(n)`
keysSet :: forall k v. Ord k => M.Map k v -> Set k
keysSet = Set <<< void
10 changes: 8 additions & 2 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import Prelude

import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Test.Assert (ASSERT, assert)

import Data.Map as M
import Data.Set (Set)
import Data.Set as S
import Data.Tuple (Tuple(..))
import Test.Assert (ASSERT, assert)

main :: Eff (console :: CONSOLE, assert :: ASSERT) Unit
main = do
Expand All @@ -26,3 +27,8 @@ main = do
s2 = S.fromFoldable [2,4,6,8,10]
s3 = S.fromFoldable [2,4]
assert $ S.intersection s1 s2 == s3

log "keysSet"
do let s1 = M.fromFoldable [Tuple 1 1, Tuple 2 2, Tuple 3 3]
s2 = S.fromFoldable [1, 2, 3]
assert $ S.keysSet s1 == s2