on master help sign in
Update to get/getSafe/getUnsafe naming scheme
ec48ed1Evan Czaplicki
libraries/Array.elm+18−15
@@ -11,7 +11,7 @@ reads, updates, and appends.
11@docs length, push, append
11@docs length, push, append
12
12
13# Get and Set
13# Get and Set
14−@docs get, getMaybe, getWithDefault, set
14+@docs get, getSafe, getUnsafe, set
15
15
16# Taking Arrays Apart
16# Taking Arrays Apart
17@docs toList, toIndexedList, slice
17@docs toList, toIndexedList, slice
@@ -120,22 +120,25 @@ empty = Native.Array.empty
120push : a -> Array a -> Array a
120push : a -> Array a -> Array a
121push = Native.Array.push
121push = Native.Array.push
122
122
123−{-| Return the element at the index. Breaks, if index is out of range. If the
123+{-| Get the element at a particular index.
124−array length is unkown, use withDefaultGet oder getWithDefault.
125
124
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.
127-}
130-}
128−get : Int -> Array a -> a
131+getUnsafe : Int -> Array a -> a
129−get = Native.Array.get
132+getUnsafe = Native.Array.get
130
133
131{-| Return Just the element at the index or Nothing if the index is out of range.
134{-| Return Just the element at the index or Nothing if the index is out of range.
132
135
133− getMaybe 2 (fromList [3,2,1]) == Just 2
136+ get 2 (fromList [3,2,1]) == Just 2
134− getMaybe 5 (fromList [3,2,1]) == Nothing
137+ get 5 (fromList [3,2,1]) == Nothing
135− getMaybe -1 (fromList [3,2,1]) == Nothing
138+ get -1 (fromList [3,2,1]) == Nothing
136-}
139-}
137−getMaybe : Int -> Array a -> Maybe a
140+get : Int -> Array a -> Maybe a
138−getMaybe i array =
141+get i array =
139 if 0 <= i && i < Native.Array.length array
142 if 0 <= i && i < Native.Array.length array
140 then Just (Native.Array.get i array)
143 then Just (Native.Array.get i array)
141 else Nothing
144 else Nothing
@@ -143,11 +146,11 @@ getMaybe i array =
143{-| Get the element at the index. If the index is out of range, the given default
146{-| Get the element at the index. If the index is out of range, the given default
144element is returned.
147element is returned.
145
148
146− getWithDefault 0 2 (fromList [3,2,1]) == 1
149+ getSafe 0 2 (fromList [3,2,1]) == 1
147− getWithDefault 0 5 (fromList [3,2,1]) == 0
150+ getSafe 0 5 (fromList [3,2,1]) == 0
148-}
151-}
149−getWithDefault : a -> Int -> Array a -> a
152+getSafe : a -> Int -> Array a -> a
150−getWithDefault default i array =
153+getSafe default i array =
151 if 0 <= i && i < Native.Array.length array
154 if 0 <= i && i < Native.Array.length array
152 then Native.Array.get i array
155 then Native.Array.get i array
153 else default
156 else default
libraries/Dict.elm+37−34
@@ -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
4 ,filter
4 ,filter
5 ,partition
5 ,partition
@@ -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"
89
89
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.
91
93
92 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
93
95
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
97
99
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.
101-}
102-}
102−get : comparable -> Dict comparable v -> v
103+get : comparable -> Dict comparable v -> Maybe v
103get k t =
104get k t =
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
111
112
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.
114−dictionary.
115
115
116 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
116 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
117
117
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
121-}
121-}
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
131
131
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.
134
133
135 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
136
135
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!
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.
140-}
143-}
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
148 EQ -> v
151 EQ -> v
149− GT -> getWithDefault base k r
152+ GT -> getUnsafe k r
150
153
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
155
158
156ensureBlackRoot : Dict k v -> Dict k v
159ensureBlackRoot : Dict k v -> Dict k v
157ensureBlackRoot t =
160ensureBlackRoot t =
tests/elm/Test/Array.elm+6−6
@@ -24,12 +24,12 @@ tests =
24 , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
24 , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
25 , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
25 , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
26 ]
26 ]
27− getAndSetTests = [ test "get" <| assertEqual (Array.get 2 (Array.fromList [3,2,1])) 1
27+ getAndSetTests = [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
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
28+ , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
30− , test "getMaybe 3" <| assertEqual (Array.getMaybe -1 (Array.fromList [3,2,1])) Nothing
29+ , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
31− , test "getWithDefault" <| assertEqual (Array.getWithDefault 0 2 (Array.fromList [3,2,1])) 1
30+ , test "getSafe 1" <| assertEqual (Array.getSafe 0 2 (Array.fromList [3,2,1])) 1
32− , test "getWithDefault 2" <| assertEqual (Array.getWithDefault 0 5 (Array.fromList [3,2,1])) 0
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
33 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
33 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
34 ]
34 ]
35 takingArraysApartTests = [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
35 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") ]
12tests : [Test]
12tests : [Test]
13tests =
13tests =
14 let getTests =
14 let getTests =
15− [ test "get" <| assertEqual "cat" (Dict.get "Tom" animals)
16− , test "getMaybe 1" <| assertEqual (Just "cat") (Dict.getMaybe "Tom" animals)
15+ [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
17− , test "getMaybe 2" <| assertEqual Nothing (Dict.getMaybe "Spike" animals)
16+ , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
18− , test "getWithDefault 1" <| assertEqual "mouse" (Dict.getWithDefault "dog" "Jerry" animals)
17+ , test "getSafe 1" <| assertEqual "mouse" (Dict.getSafe "dog" "Jerry" animals)
19− , test "getWithDefault 2" <| assertEqual "dog" (Dict.getWithDefault "dog" "Spike" animals)
18+ , test "getSafe 2" <| assertEqual "dog" (Dict.getSafe "dog" "Spike" animals)
19+ , test "getUnsafe" <| assertEqual "cat" (Dict.getUnsafe "Tom" animals)
20 ]
20 ]
21 filterTests =
21 filterTests =
22 [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)
22 [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)