on master help sign in
Update to get/getSafe/getUnsafe naming scheme
commitec48ed160add045626fc81d6a2cb856d713e43c0 authorEvan Czaplicki <info@elm-lang.org> dateFri 18 Apr 2014, 11:44 (13 years ago) parent1cc3bac
4 files changed+66−60
libraries/Array.elm+18−15 libraries/Dict.elm+37−34 tests/elm/Test/Array.elm+6−6 tests/elm/Test/Dict.elm+5−5
libraries/Array.elm+18−15
@@ -11,7 +11,7 @@ reads, updates, and appends.
1111@docs length, push, append
1212
1313# Get and Set
14−@docs get, getMaybe, getWithDefault, set
14+@docs get, getSafe, getUnsafe, set
1515
1616# Taking Arrays Apart
1717@docs toList, toIndexedList, slice
@@ -120,22 +120,25 @@ empty = Native.Array.empty
120120push : a -> Array a -> Array a
121121push = Native.Array.push
122122
123−{-| Return the element at the index. Breaks, if index is out of range. If the
124−array length is unkown, use withDefaultGet oder getWithDefault.
123+{-| Get the element at a particular index.
125124
126− get 2 (A.fromList [3,2,1]) == 1
125+ getUnsafe 2 (A.fromList [3,2,1]) == 1
126+
127+Warning: this function will result in a runtime error if the index is not found,
128+so it is best to use `get` or `getSafe` unless you are very confident the index
129+will be found.
127130-}
128−get : Int -> Array a -> a
129−get = Native.Array.get
131+getUnsafe : Int -> Array a -> a
132+getUnsafe = Native.Array.get
130133
131134{-| Return Just the element at the index or Nothing if the index is out of range.
132135
133− getMaybe 2 (fromList [3,2,1]) == Just 2
134− getMaybe 5 (fromList [3,2,1]) == Nothing
135− getMaybe -1 (fromList [3,2,1]) == Nothing
136+ get 2 (fromList [3,2,1]) == Just 2
137+ get 5 (fromList [3,2,1]) == Nothing
138+ get -1 (fromList [3,2,1]) == Nothing
136139-}
137−getMaybe : Int -> Array a -> Maybe a
138−getMaybe i array =
140+get : Int -> Array a -> Maybe a
141+get i array =
139142 if 0 <= i && i < Native.Array.length array
140143 then Just (Native.Array.get i array)
141144 else Nothing
@@ -143,11 +146,11 @@ getMaybe i array =
143146{-| Get the element at the index. If the index is out of range, the given default
144147element is returned.
145148
146− getWithDefault 0 2 (fromList [3,2,1]) == 1
147− getWithDefault 0 5 (fromList [3,2,1]) == 0
149+ getSafe 0 2 (fromList [3,2,1]) == 1
150+ getSafe 0 5 (fromList [3,2,1]) == 0
148151-}
149−getWithDefault : a -> Int -> Array a -> a
150−getWithDefault default i array =
152+getSafe : a -> Int -> Array a -> a
153+getSafe default i array =
151154 if 0 <= i && i < Native.Array.length array
152155 then Native.Array.get i array
153156 else default
libraries/Dict.elm+37−34
@@ -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 =
tests/elm/Test/Array.elm+6−6
@@ -24,12 +24,12 @@ tests =
2424 , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
2525 , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
2626 ]
27− getAndSetTests = [ test "get" <| assertEqual (Array.get 2 (Array.fromList [3,2,1])) 1
28− , test "getMaybe" <| assertEqual (Array.getMaybe 1 (Array.fromList [3,2,1])) (Just 2)
29− , test "getMaybe 2" <| assertEqual (Array.getMaybe 5 (Array.fromList [3,2,1])) Nothing
30− , test "getMaybe 3" <| assertEqual (Array.getMaybe -1 (Array.fromList [3,2,1])) Nothing
31− , test "getWithDefault" <| assertEqual (Array.getWithDefault 0 2 (Array.fromList [3,2,1])) 1
32− , test "getWithDefault 2" <| assertEqual (Array.getWithDefault 0 5 (Array.fromList [3,2,1])) 0
27+ getAndSetTests = [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
28+ , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
29+ , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
30+ , test "getSafe 1" <| assertEqual (Array.getSafe 0 2 (Array.fromList [3,2,1])) 1
31+ , test "getSafe 2" <| assertEqual (Array.getSafe 0 5 (Array.fromList [3,2,1])) 0
32+ , test "getUnsafe" <| assertEqual (Array.getUnsafe 2 (Array.fromList [3,2,1])) 1
3333 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
3434 ]
3535 takingArraysApartTests = [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
tests/elm/Test/Dict.elm+5−5
@@ -12,11 +12,11 @@ animals = Dict.fromList [ ("Tom", "cat"), ("Jerry", "mouse") ]
1212tests : [Test]
1313tests =
1414 let getTests =
15− [ test "get" <| assertEqual "cat" (Dict.get "Tom" animals)
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)
15+ [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
16+ , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
17+ , test "getSafe 1" <| assertEqual "mouse" (Dict.getSafe "dog" "Jerry" animals)
18+ , test "getSafe 2" <| assertEqual "dog" (Dict.getSafe "dog" "Spike" animals)
19+ , test "getUnsafe" <| assertEqual "cat" (Dict.getUnsafe "Tom" animals)
2020 ]
2121 filterTests =
2222 [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)