@@ -1,5 +1,5 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2− ,lookup,findWithDefault
2+ ,get,getMaybe,getWithDefault
3 ,remove,member
3 ,remove,member
@@ -19,7 +19,7 @@ Insert, remove, and query operations all take *O(log n)* time.
19@docs empty, singleton, insert, update, remove
19@docs empty, singleton, insert, update, remove
22−@docs member, lookup, findWithDefault
22+@docs member, get, getMaybe, getWithDefault
25@docs union, intersect, diff
25@docs union, intersect, diff
@@ -37,6 +37,7 @@ import Basics (..)
37import Maybe (..)
37import Maybe (..)
38import Native.Error
38import Native.Error
39import List
39import List
40import Native.Utils
41import Native.Utils
42-- BBlack and NBlack should only be used during the deletion
43-- BBlack and NBlack should only be used during the deletion
@@ -86,33 +87,71 @@ max t =
86 RBNode _ _ _ _ r -> max r
87 RBNode _ _ _ _ r -> max r
87 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.
92+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94+ get "Tom" animals == Cat
95+ get "Mouse" animals == Mouse
96+ get "Spike" animals -- Runtime Error!
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.
102+get : comparable -> Dict comparable v -> v
105+ RBEmpty LBlack -> Native.Error.raise <| String.concat [ "key ", String.show k, " not found." ]
106+ RBNode _ k' v l r ->
107+ case Native.Utils.compare k k' of
89−{-| Lookup the value associated with a key. -}
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
116+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
118+ getMaybe "Tom" animals == Just Cat
119+ getMaybe "Mouse" animals == Just Mouse
120+ getMaybe "Spike" animals == Nothing
90−lookup : comparable -> Dict comparable v -> Maybe v
122+getMaybe : comparable -> Dict comparable v -> Maybe v
91−lookup k t =
123+getMaybe k t =
92 case t of
124 case t of
93 RBEmpty LBlack -> Nothing
125 RBEmpty LBlack -> Nothing
94 RBNode _ k' v l r ->
126 RBNode _ k' v l r ->
95 case Native.Utils.compare k k' of
127 case Native.Utils.compare k k' of
96− LT -> lookup k l
128+ LT -> getMaybe k l
97 EQ -> Just v
129 EQ -> Just v
98− GT -> lookup k r
130+ GT -> getMaybe k r
100−{-| Find the value associated with a key. If the key is not found,
132+{-| Get the value associated with a key. If the key is not found,
101−return the default value. -}
133+return a default value.
135+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
137+ getWithDefault Dog "Tom" animals == Cat
138+ getWithDefault Dog "Mouse" animals == Mouse
139+ getWithDefault Dog "Spike" animals == Dog
102−findWithDefault : v -> comparable -> Dict comparable v -> v
141+getWithDefault : v -> comparable -> Dict comparable v -> v
103−findWithDefault base k t =
142+getWithDefault base k t =
104 case t of
143 case t of
105 RBEmpty LBlack -> base
144 RBEmpty LBlack -> base
106 RBNode _ k' v l r ->
145 RBNode _ k' v l r ->
107 case Native.Utils.compare k k' of
146 case Native.Utils.compare k k' of
108− LT -> findWithDefault base k l
147+ LT -> getWithDefault base k l
110− GT -> findWithDefault base k r
149+ GT -> getWithDefault base k r
112{-| Determine if a key is in a dictionary. -}
151{-| Determine if a key is in a dictionary. -}
113member : comparable -> Dict comparable v -> Bool
152member : comparable -> Dict comparable v -> Bool
114-- Does t contain k?
153-- Does t contain k?
115−member k t = isJust <| lookup k t
154+member k t = isJust <| getMaybe k t
117ensureBlackRoot : Dict k v -> Dict k v
156ensureBlackRoot : Dict k v -> Dict k v
118ensureBlackRoot t =
157ensureBlackRoot t =
@@ -358,8 +397,10 @@ filter p dict =
358 let add k v t = if p k v then insert k v t else t
397 let add k v t = if p k v then insert k v t else t
359 in foldl add empty dict
398 in foldl add empty dict
361−{-| Partition the Dict according to a predicate. The first Dict contains all
400+{-| Partition a dictionary according to a predicate. The first dictionary
362−key-value pairs which satisfy it; the second contains the rest. -}
401+contains all key-value pairs which satisfy the predicate, and the second
363partition : (comparable -> v -> Bool) -> Dict comparable v -> (Dict comparable v, Dict comparable v)
404partition : (comparable -> v -> Bool) -> Dict comparable v -> (Dict comparable v, Dict comparable v)
364partition p dict =
405partition p dict =
365 let add k v (t1, t2) = if p k v
406 let add k v (t1, t2) = if p k v