@@ -1,5 +1,5 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2− ,get,getMaybe,getWithDefault
2+ ,get,getSafe,getUnsafe
3 ,remove,member
3 ,remove,member
@@ -87,71 +87,74 @@ max t =
87 RBNode _ _ _ _ r -> max r
87 RBNode _ _ _ _ r -> max r
88 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
88 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
90−{-| Get the value associated with a key.
90+{-| Get the value associated with a key. If the key is not found, return
91+`Nothing`. This is useful when you are not sure if a key will be in the
92 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94− get "Tom" animals == Cat
96+ get "Tom" animals == Just Cat
95− get "Mouse" animals == Mouse
97+ get "Mouse" animals == Just Mouse
96− get "Spike" animals -- Runtime Error!
98+ get "Spike" animals == Nothing
98−Warning: this function will result in a runtime error if the key is not found,
100+The `getSafe` and `getUnsafe` are built-in ways to handle common ways of
99−so it is best to use `getMaybe` or `getWithDefault` unless you are very
101+using the resulting `Maybe`.
100−confident the key will be found.
102−get : comparable -> Dict comparable v -> v
103+get : comparable -> Dict comparable v -> Maybe v
104 case t of
105 case t of
105− RBEmpty LBlack -> Native.Error.raise <| String.concat [ "key ", String.show k, " not found." ]
106+ RBEmpty LBlack -> Nothing
106 RBNode _ k' v l r ->
107 RBNode _ k' v l r ->
107 case Native.Utils.compare k k' of
108 case Native.Utils.compare k k' of
108 LT -> get k l
109 LT -> get k l
109− EQ -> v
110+ EQ -> Just v
110 GT -> get k r
111 GT -> get k r
112−{-| Get the value associated with a key. If the key is not found, return
113+{-| Get the value associated with a key. If the key is not found,
113−`Nothing`. This is useful when you are not sure if a key will be in the
114+return a default value.
116 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
116 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
118− getMaybe "Tom" animals == Just Cat
118+ getSafe Dog "Tom" animals == Cat
119− getMaybe "Mouse" animals == Just Mouse
119+ getSafe Dog "Mouse" animals == Mouse
120− getMaybe "Spike" animals == Nothing
120+ getSafe Dog "Spike" animals == Dog
122−getMaybe : comparable -> Dict comparable v -> Maybe v
122+getSafe : v -> comparable -> Dict comparable v -> v
123−getMaybe k t =
123+getSafe base k t =
124 case t of
124 case t of
125− RBEmpty LBlack -> Nothing
125+ RBEmpty LBlack -> base
126 RBNode _ k' v l r ->
126 RBNode _ k' v l r ->
127 case Native.Utils.compare k k' of
127 case Native.Utils.compare k k' of
128− LT -> getMaybe k l
128+ LT -> getSafe base k l
129− EQ -> Just v
129+ EQ -> v
130− GT -> getMaybe k r
130+ GT -> getSafe base k r
132−{-| Get the value associated with a key. If the key is not found,
132+{-| Get the value associated with a key.
133−return a default value.
135 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
137− getWithDefault Dog "Tom" animals == Cat
136+ get "Tom" animals == Cat
138− getWithDefault Dog "Mouse" animals == Mouse
137+ get "Mouse" animals == Mouse
139− getWithDefault Dog "Spike" animals == Dog
138+ get "Spike" animals == -- Runtime Error!
140+Warning: this function will result in a runtime error if the key is not found,
141+so it is best to use `get` or `getSafe` unless you are very confident the key
141−getWithDefault : v -> comparable -> Dict comparable v -> v
144+getUnsafe : comparable -> Dict comparable v -> v
142−getWithDefault base k t =
145+getUnsafe k t =
143 case t of
146 case t of
144− RBEmpty LBlack -> base
147+ RBEmpty LBlack -> Native.Error.raise <| String.concat [ "key ", String.show k, " not found." ]
145 RBNode _ k' v l r ->
148 RBNode _ k' v l r ->
146 case Native.Utils.compare k k' of
149 case Native.Utils.compare k k' of
147− LT -> getWithDefault base k l
150+ LT -> getUnsafe k l
149− GT -> getWithDefault base k r
152+ GT -> getUnsafe k r
151{-| Determine if a key is in a dictionary. -}
154{-| Determine if a key is in a dictionary. -}
152member : comparable -> Dict comparable v -> Bool
155member : comparable -> Dict comparable v -> Bool
153-- Does t contain k?
156-- Does t contain k?
154−member k t = isJust <| getMaybe k t
157+member k t = isJust <| get k t
156ensureBlackRoot : Dict k v -> Dict k v
159ensureBlackRoot : Dict k v -> Dict k v
157ensureBlackRoot t =
160ensureBlackRoot t =