on master help sign in
Merge pull request #3771 from Synthetica9/SortedSet_TODO_Done

Added `union`, `intersection`, `difference` and `symDifference`

commit9beee44dda13a5e4780551d20c21bc3d2cd4866c authorAhmad Salim Al-Sibahi <asal@itu.dk> dateMon 24 Apr 2017, 11:27 (9 years ago) parentd0fc5e7f1c4090
2 files changed+28−5
libs/contrib/Data/SortedMap.idr+1−2 libs/contrib/Data/SortedSet.idr+27−3
libs/contrib/Data/SortedMap.idr+1−2
@@ -238,11 +238,10 @@ toList (M _ t) = treeToList t
238238treeMap : (a -> b) -> Tree n k a o -> Tree n k b o
239239treeMap f (Leaf k v) = Leaf k (f v)
240240treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
241−treeMap f (Branch3 t1 k1 t2 k2 t3)
241+treeMap f (Branch3 t1 k1 t2 k2 t3)
242242 = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
243243
244244export
245245implementation Functor (SortedMap k) where
246246 map _ Empty = Empty
247247 map f (M n t) = M _ (treeMap f t)
248−
libs/contrib/Data/SortedSet.idr+27−3
@@ -2,8 +2,6 @@ module Data.SortedSet
22
33import Data.SortedMap
44
5−-- TODO: add intersection, union, difference
6−
75export
86data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ())
97
@@ -31,5 +29,31 @@ export
3129toList : SortedSet k -> List k
3230toList (SetWrapper m) = map (\(i, _) => i) (Data.SortedMap.toList m)
3331
34−implementation Foldable SortedSet where
32+Foldable SortedSet where
3533 foldr f e xs = foldr f e (Data.SortedSet.toList xs)
34+
35+||| Set union. Inserts all elements of x into y
36+export
37+union : (x, y : SortedSet k) -> SortedSet k
38+union x y = foldr insert x y
39+
40+||| Set difference. Delete all elments in y from x
41+export
42+difference : (x, y : SortedSet k) -> SortedSet k
43+difference x y = foldr delete x y
44+
45+||| Set symmetric difference. Uses the union of the differences.
46+export
47+symDifference : (x, y : SortedSet k) -> SortedSet k
48+symDifference x y = union (difference x y) (difference y x)
49+
50+||| Set intersection. Implemented as the difference of the union and the symetric difference.
51+export
52+intersection : (x, y : SortedSet k) -> SortedSet k
53+intersection x y = difference x (difference x y)
54+
55+Ord k => Semigroup (SortedSet k) where
56+ (<+>) = union
57+
58+Ord k => Monoid (SortedSet k) where
59+ neutral = empty