on master help sign in
Add "equals" functions for Set, Dict, and Array
commit45aa58f9d40b99bdad64e4201ebd37e6f756b2f8 authorEvan Czaplicki <info@elm-lang.org> dateMon 14 Apr 2014, 22:03 (13 years ago) parent89fda74
3 files changed+38−5
libraries/Array.elm+17−1 libraries/Dict.elm+14−2 libraries/Set.elm+7−2
libraries/Array.elm+17−1
@@ -8,7 +8,7 @@ reads, updates, and appends.
88@docs empty, repeat, initialize, fromList
99
1010# Basics
11−@docs length, push, append
11+@docs length, equals, push, append
1212
1313# Get and Set
1414@docs get, getMaybe, getWithDefault, set
@@ -103,6 +103,22 @@ foldr = Native.Array.foldr
103103empty : Array a
104104empty = Native.Array.empty
105105
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+
106122{-| Push an element to the end of an array.
107123
108124 push 3 (fromList [1,2]) == fromList [1,2,3]
libraries/Dict.elm+14−2
@@ -1,5 +1,5 @@
11module Dict (empty,singleton,insert,update
2− ,lookup,findWithDefault
2+ ,lookup,findWithDefault,equals
33 ,remove,member
44 ,foldl,foldr,map
55 ,union,intersect,diff
@@ -17,7 +17,7 @@ Insert, remove, and query operations all take *O(log n)* time.
1717@docs empty, singleton, insert, update, remove
1818
1919# Query
20−@docs member, lookup, findWithDefault
20+@docs member, lookup, findWithDefault, equals
2121
2222# Combine
2323@docs union, intersect, diff
@@ -112,6 +112,18 @@ member : comparable -> Dict comparable v -> Bool
112112-- Does t contain k?
113113member k t = isJust <| lookup k t
114114
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+
115127ensureBlackRoot : Dict k v -> Dict k v
116128ensureBlackRoot t =
117129 case t of
libraries/Set.elm+7−2
@@ -1,6 +1,6 @@
11
22module Set (empty,singleton,insert,remove
3− ,member
3+ ,member,equals
44 ,foldl,foldr,map
55 ,union,intersect,diff
66 ,toList,fromList
@@ -16,7 +16,7 @@ Insert, remove, and query operations all take *O(log n)* time.
1616@docs empty, singleton, insert, remove
1717
1818# Query
19−@docs member
19+@docs member, equals
2020
2121# Combine
2222@docs union, intersect, diff
@@ -56,6 +56,11 @@ remove = Dict.remove
5656member : comparable -> Set comparable -> Bool
5757member = Dict.member
5858
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+
5964{-| Get the union of two sets. Keep all values. -}
6065union : Set comparable -> Set comparable -> Set comparable
6166union = Dict.union