on master help sign in
Make lookup names to be consistent with Array
1cc3bacEvan Czaplicki
libraries/Dict.elm+57−16
@@ -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
4 ,filter
4 ,filter
5 ,partition
5 ,partition
@@ -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
20
20
21# Query
21# Query
22−@docs member, lookup, findWithDefault
22+@docs member, get, getMaybe, getWithDefault
23
23
24# Combine
24# Combine
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
40+import String
40import Native.Utils
41import Native.Utils
41
42
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"
88
89
90+{-| Get the value associated with a key.
91+
92+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
93+
94+ get "Tom" animals == Cat
95+ get "Mouse" animals == Mouse
96+ get "Spike" animals -- Runtime Error!
97+
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.
101+-}
102+get : comparable -> Dict comparable v -> v
103+get k t =
104+ case t of
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
108+ LT -> get k l
109+ EQ -> v
110+ GT -> get k r
111+
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
114+dictionary.
115+
116+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
117+
118+ getMaybe "Tom" animals == Just Cat
119+ getMaybe "Mouse" animals == Just Mouse
120+ getMaybe "Spike" animals == Nothing
121+-}
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
99
131
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.
134+
135+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
136+
137+ getWithDefault Dog "Tom" animals == Cat
138+ getWithDefault Dog "Mouse" animals == Mouse
139+ getWithDefault Dog "Spike" animals == Dog
140+-}
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
109 EQ -> v
148 EQ -> v
110− GT -> findWithDefault base k r
149+ GT -> getWithDefault base k r
111
150
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
116
155
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
360
399
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
402+contains the rest.
403+-}
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
tests/elm/Test.elm+2−1
@@ -12,9 +12,10 @@ import IO.Runner as Run
12import Test.String as String
12import Test.String as String
13import Test.Trampoline as Trampoline
13import Test.Trampoline as Trampoline
14import Test.Array as Array
14import Test.Array as Array
15+import Test.Dict as Dict
15
16
16tests : [Test]
17tests : [Test]
17−tests = String.tests ++ Trampoline.tests ++ Array.tests
18+tests = String.tests ++ Trampoline.tests ++ Array.tests ++ Dict.tests
18
19
19console : IO ()
20console : IO ()
20console = runDisplay tests
21console = runDisplay tests
tests/elm/Test/Dict.elm+15−18
@@ -2,28 +2,25 @@ module Test.Dict (tests) where
2
2
3import Dict
3import Dict
4import List
4import List
5−import Char
6
5
7import ElmTest.Assertion (..)
6import ElmTest.Assertion (..)
8import ElmTest.Test (..)
7import ElmTest.Test (..)
9
8
10−parseDigit : Char -> Int
9+animals : Dict.Dict String String
11−parseDigit ch = Char.toCode ch - 48
10+animals = Dict.fromList [ ("Tom", "cat"), ("Jerry", "mouse") ]
12−
13−dict1List = [(1,'1'),(2,'2'),(3,'3'),(5,'5')]
14−
15−dict1 : Dict Int Char
16−dict1 = Dict.fromList dict1List
17−
18−dict1Part1 : Dict Int Char
19−dict1Part1 = Dict.fromList (take 2 dict1List)
20−
21−dict1Part2 : Dict Int Char
22−dict1Part2 = Dict.fromList (drop 2 dict1List)
23
11
24tests : [Test]
12tests : [Test]
25tests =
13tests =
26− let
14+ let getTests =
27− filterTests = [assertEqual dict1Part2 <| filter (\v -> parseDigit v > 2) dict1]
15+ [ test "get" <| assertEqual "cat" (Dict.get "Tom" animals)
28− partitionTests = [assertEqual (dict1Part1, dict1Part2) <| partition (\v -> v != '3' && v != '5') dict1]
16+ , test "getMaybe 1" <| assertEqual (Just "cat") (Dict.getMaybe "Tom" animals)
17+ , test "getMaybe 2" <| assertEqual Nothing (Dict.getMaybe "Spike" animals)
18+ , test "getWithDefault 1" <| assertEqual "mouse" (Dict.getWithDefault "dog" "Jerry" animals)
19+ , test "getWithDefault 2" <| assertEqual "dog" (Dict.getWithDefault "dog" "Spike" animals)
20+ ]
21+ filterTests =
22+ [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)
23+ , test "partition" <| assertEqual (Dict.singleton "Tom" "cat", Dict.singleton "Jerry" "mouse") (Dict.partition (\k v -> k == "Tom") animals)
24+ ]
25+ in
29− in List.concat [filterTests, partitionTests]
26+ List.concat [ getTests, filterTests ]