on master help sign in
Add "equals" functions for Set, Dict, and Array
45aa58fEvan Czaplicki
libraries/Array.elm+17−1
@@ -8,7 +8,7 @@ reads, updates, and appends.
8@docs empty, repeat, initialize, fromList
8@docs empty, repeat, initialize, fromList
9
9
10# Basics
10# Basics
11−@docs length, push, append
11+@docs length, equals, push, append
12
12
13# Get and Set
13# Get and Set
14@docs get, getMaybe, getWithDefault, set
14@docs get, getMaybe, getWithDefault, set
@@ -103,6 +103,22 @@ foldr = Native.Array.foldr
103empty : Array a
103empty : Array a
104empty = Native.Array.empty
104empty = Native.Array.empty
105
105
106+{-| Check if all values of the array are equal. True if both arrays are the same
107+length and the values are equal at every index.
108+
109+ equals (fromList [2,2]) (repeat 2 2) == True
110+ equals (fromList [1,2]) (repeat 2 2) == False
111+-}
112+equals : Array a -> Array a -> Bool
113+equals a b =
114+ let go lists =
115+ case lists of
116+ (x::xs,y::ys) -> x == y && go (xs,ys)
117+ ([],[]) -> True
118+ _ -> False
119+ in
120+ go (Native.Array.toList a, Native.Array.toList b)
121+
106{-| Push an element to the end of an array.
122{-| Push an element to the end of an array.
107
123
108 push 3 (fromList [1,2]) == fromList [1,2,3]
124 push 3 (fromList [1,2]) == fromList [1,2,3]
libraries/Dict.elm+14−2
@@ -1,5 +1,5 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2− ,lookup,findWithDefault
2+ ,lookup,findWithDefault,equals
3 ,remove,member
3 ,remove,member
4 ,foldl,foldr,map
4 ,foldl,foldr,map
5 ,union,intersect,diff
5 ,union,intersect,diff
@@ -17,7 +17,7 @@ Insert, remove, and query operations all take *O(log n)* time.
17@docs empty, singleton, insert, update, remove
17@docs empty, singleton, insert, update, remove
18
18
19# Query
19# Query
20−@docs member, lookup, findWithDefault
20+@docs member, lookup, findWithDefault, equals
21
21
22# Combine
22# Combine
23@docs union, intersect, diff
23@docs union, intersect, diff
@@ -112,6 +112,18 @@ member : comparable -> Dict comparable v -> Bool
112-- Does t contain k?
112-- Does t contain k?
113member k t = isJust <| lookup k t
113member k t = isJust <| lookup k t
114
114
115+{-| Check if the contents of all keys and values are equal.
116+-}
117+equals : Dict comparable v -> Dict comparable v -> Bool
118+equals a b =
119+ let go lists =
120+ case lists of
121+ (x::xs,y::ys) -> x == y && go (xs,ys)
122+ ([],[]) -> True
123+ _ -> False
124+ in
125+ go (toList a, toList b)
126+
115ensureBlackRoot : Dict k v -> Dict k v
127ensureBlackRoot : Dict k v -> Dict k v
116ensureBlackRoot t =
128ensureBlackRoot t =
117 case t of
129 case t of
libraries/Set.elm+7−2
@@ -1,6 +1,6 @@
1
1
2module Set (empty,singleton,insert,remove
2module Set (empty,singleton,insert,remove
3− ,member
3+ ,member,equals
4 ,foldl,foldr,map
4 ,foldl,foldr,map
5 ,union,intersect,diff
5 ,union,intersect,diff
6 ,toList,fromList
6 ,toList,fromList
@@ -16,7 +16,7 @@ Insert, remove, and query operations all take *O(log n)* time.
16@docs empty, singleton, insert, remove
16@docs empty, singleton, insert, remove
17
17
18# Query
18# Query
19−@docs member
19+@docs member, equals
20
20
21# Combine
21# Combine
22@docs union, intersect, diff
22@docs union, intersect, diff
@@ -56,6 +56,11 @@ remove = Dict.remove
56member : comparable -> Set comparable -> Bool
56member : comparable -> Set comparable -> Bool
57member = Dict.member
57member = Dict.member
58
58
59+{-| Check if the sets are the same size and all elements are equal.
60+-}
61+equals : Set comparable -> Set comparable -> Bool
62+equals = Dict.equals
63+
59{-| Get the union of two sets. Keep all values. -}
64{-| Get the union of two sets. Keep all values. -}
60union : Set comparable -> Set comparable -> Set comparable
65union : Set comparable -> Set comparable -> Set comparable
61union = Dict.union
66union = Dict.union