-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathSet.hs
More file actions
59 lines (39 loc) · 1.47 KB
/
Set.hs
File metadata and controls
59 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Set where
import Data.Hashable
import qualified Data.Set as S
newtype Set v = Set {unSet :: S.Set v} deriving (Show, Eq, Foldable, Ord)
instance Hashable v => Hashable (Set v) where
hashWithSalt s = hashWithSalt s . Set.toList
instance Ord a => Semigroup (Set a) where
Set s1 <> Set s2 = Set (s1 <> s2)
toList :: Set v -> [v]
toList (Set s) = S.toList s
fromList :: Ord v => [v] -> Set v
fromList = Set . S.fromList
empty :: Set v
empty = Set S.empty
intersection :: Ord v => Set v -> Set v -> Set v
intersection (Set a) (Set b) = Set (S.intersection a b)
union :: Ord v => Set v -> Set v -> Set v
union (Set a) (Set b) = Set (S.union a b)
unions :: (Functor f, Foldable f, Ord a) => f (Set a) -> Set a
unions sets = Set $ S.unions (fmap unSet sets)
member :: Ord v => v -> Set v -> Bool
member k (Set s) = S.member k s
notMember :: Ord v => v -> Set v -> Bool
notMember k (Set s) = S.notMember k s
insert :: Ord v => v -> Set v -> Set v
insert v (Set s) = Set $ S.insert v s
(\\) :: Ord v => Set v -> Set v -> Set v
(\\) (Set a) (Set b) = Set ((S.\\) a b)
delete :: Ord v => v -> Set v -> Set v
delete v (Set s) = Set $ S.delete v s
filter :: Ord v => (v -> Bool) -> Set v -> Set v
filter f (Set s) = Set $ S.filter f s
size :: Set v -> Int
size (Set s) = S.size s
map :: Ord b => (a -> b) -> Set a -> Set b
map f (Set s) = Set $ S.map f s
isSubsetOf :: Ord v => Set v -> Set v -> Bool
isSubsetOf (Set x) (Set y) = x `S.isSubsetOf` y