@@ -1,5 +1,5 @@
11module Dict (empty,singleton,insert,update
2− ,get,getMaybe,getWithDefault
2+ ,get,getSafe,getUnsafe
33 ,remove,member
44 ,filter
55 ,partition
@@ -87,71 +87,74 @@ max t =
8787 RBNode _ _ _ _ r -> max r
8888 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
8989
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+dictionary.
9193
9294 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
9395
94− get "Tom" animals == Cat
95− get "Mouse" animals == Mouse
96− get "Spike" animals -- Runtime Error!
96+ get "Tom" animals == Just Cat
97+ get "Mouse" animals == Just Mouse
98+ get "Spike" animals == Nothing
9799
98−Warning: this function will result in a runtime error if the key is not found,
99−so it is best to use `getMaybe` or `getWithDefault` unless you are very
100−confident the key will be found.
100+The `getSafe` and `getUnsafe` are built-in ways to handle common ways of
101+using the resulting `Maybe`.
101102-}
102−get : comparable -> Dict comparable v -> v
103+get : comparable -> Dict comparable v -> Maybe v
103104get k t =
104105 case t of
105− RBEmpty LBlack -> Native.Error.raise <| String.concat [ "key ", String.show k, " not found." ]
106+ RBEmpty LBlack -> Nothing
106107 RBNode _ k' v l r ->
107108 case Native.Utils.compare k k' of
108109 LT -> get k l
109− EQ -> v
110+ EQ -> Just v
110111 GT -> get k r
111112
112−{-| Get the value associated with a key. If the key is not found, return
113−`Nothing`. This is useful when you are not sure if a key will be in the
114−dictionary.
113+{-| Get the value associated with a key. If the key is not found,
114+return a default value.
115115
116116 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
117117
118− getMaybe "Tom" animals == Just Cat
119− getMaybe "Mouse" animals == Just Mouse
120− getMaybe "Spike" animals == Nothing
118+ getSafe Dog "Tom" animals == Cat
119+ getSafe Dog "Mouse" animals == Mouse
120+ getSafe Dog "Spike" animals == Dog
121121-}
122−getMaybe : comparable -> Dict comparable v -> Maybe v
123−getMaybe k t =
122+getSafe : v -> comparable -> Dict comparable v -> v
123+getSafe base k t =
124124 case t of
125− RBEmpty LBlack -> Nothing
125+ RBEmpty LBlack -> base
126126 RBNode _ k' v l r ->
127127 case Native.Utils.compare k k' of
128− LT -> getMaybe k l
129− EQ -> Just v
130− GT -> getMaybe k r
128+ LT -> getSafe base k l
129+ EQ -> v
130+ GT -> getSafe base k r
131131
132−{-| Get the value associated with a key. If the key is not found,
133−return a default value.
132+{-| Get the value associated with a key.
134133
135134 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
136135
137− getWithDefault Dog "Tom" animals == Cat
138− getWithDefault Dog "Mouse" animals == Mouse
139− getWithDefault Dog "Spike" animals == Dog
136+ get "Tom" animals == Cat
137+ get "Mouse" animals == Mouse
138+ get "Spike" animals == -- Runtime Error!
139+
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
142+will be found.
140143-}
141−getWithDefault : v -> comparable -> Dict comparable v -> v
142−getWithDefault base k t =
144+getUnsafe : comparable -> Dict comparable v -> v
145+getUnsafe k t =
143146 case t of
144− RBEmpty LBlack -> base
147+ RBEmpty LBlack -> Native.Error.raise <| String.concat [ "key ", String.show k, " not found." ]
145148 RBNode _ k' v l r ->
146149 case Native.Utils.compare k k' of
147− LT -> getWithDefault base k l
150+ LT -> getUnsafe k l
148151 EQ -> v
149− GT -> getWithDefault base k r
152+ GT -> getUnsafe k r
150153
151154{-| Determine if a key is in a dictionary. -}
152155member : comparable -> Dict comparable v -> Bool
153156-- Does t contain k?
154−member k t = isJust <| getMaybe k t
157+member k t = isJust <| get k t
155158
156159ensureBlackRoot : Dict k v -> Dict k v
157160ensureBlackRoot t =