on master help sign in
Merge pull request #3771 from Synthetica9/SortedSet_TODO_Done
9beee44Ahmad Salim Al-Sibahi
libs/contrib/Data/SortedMap.idr+1−2
@@ -238,11 +238,10 @@ toList (M _ t) = treeToList t
238treeMap : (a -> b) -> Tree n k a o -> Tree n k b o
238treeMap : (a -> b) -> Tree n k a o -> Tree n k b o
239treeMap f (Leaf k v) = Leaf k (f v)
239treeMap f (Leaf k v) = Leaf k (f v)
240treeMap f (Branch2 t1 k t2) = Branch2 (treeMap f t1) k (treeMap f t2)
240treeMap 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)
242 = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
242 = Branch3 (treeMap f t1) k1 (treeMap f t2) k2 (treeMap f t3)
243
243
244export
244export
245implementation Functor (SortedMap k) where
245implementation Functor (SortedMap k) where
246 map _ Empty = Empty
246 map _ Empty = Empty
247 map f (M n t) = M _ (treeMap f t)
247 map f (M n t) = M _ (treeMap f t)
248−
libs/contrib/Data/SortedSet.idr+27−3
@@ -2,8 +2,6 @@ module Data.SortedSet
2
2
3import Data.SortedMap
3import Data.SortedMap
4
4
5−-- TODO: add intersection, union, difference
6−
7export
5export
8data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ())
6data SortedSet k = SetWrapper (Data.SortedMap.SortedMap k ())
9
7
@@ -31,5 +29,31 @@ export
31toList : SortedSet k -> List k
29toList : SortedSet k -> List k
32toList (SetWrapper m) = map (\(i, _) => i) (Data.SortedMap.toList m)
30toList (SetWrapper m) = map (\(i, _) => i) (Data.SortedMap.toList m)
33
31
34−implementation Foldable SortedSet where
32+Foldable SortedSet where
35 foldr f e xs = foldr f e (Data.SortedSet.toList xs)
33 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