on master help sign in
Switch to the getOrElse/getOrFail name scheme
90f1956Evan Czaplicki
libraries/Array.elm+12−12
@@ -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, getSafe, getUnsafe, set
14+@docs get, getOrElse, getOrFail, set
15
15
16# Taking Arrays Apart
16# Taking Arrays Apart
17@docs toList, toIndexedList, slice
17@docs toList, toIndexedList, slice
@@ -122,14 +122,14 @@ push = Native.Array.push
122
122
123{-| Get the element at a particular index.
123{-| Get the element at a particular index.
124
124
125− getUnsafe 2 (A.fromList [3,2,1]) == 1
125+ getOrFail 2 (A.fromList [3,2,1]) == 1
126
126
127Warning: this function will result in a runtime error if the index is not found,
127Warning: 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
128+so it is best to use `get` or `getOrElse` unless you are sure the index will be
129−will be found.
129+found.
130-}
130-}
131−getUnsafe : Int -> Array a -> a
131+getOrFail : Int -> Array a -> a
132−getUnsafe = Native.Array.get
132+getOrFail = Native.Array.get
133
133
134{-| 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.
135
135
@@ -143,14 +143,14 @@ get i array =
143 then Just (Native.Array.get i array)
143 then Just (Native.Array.get i array)
144 else Nothing
144 else Nothing
145
145
146−{-| Get the element at the index. If the index is out of range, the given default
146+{-| Get the element at the index. Or if the index is out of range, a default
147−element is returned.
147+value is returned.
148
148
149− getSafe 0 2 (fromList [3,2,1]) == 1
149+ getOrElse 0 2 (fromList [3,2,1]) == 1
150− getSafe 0 5 (fromList [3,2,1]) == 0
150+ getOrElse 0 5 (fromList [3,2,1]) == 0
151-}
151-}
152−getSafe : a -> Int -> Array a -> a
152+getOrElse : a -> Int -> Array a -> a
153−getSafe default i array =
153+getOrElse default i array =
154 if 0 <= i && i < Native.Array.length array
154 if 0 <= i && i < Native.Array.length array
155 then Native.Array.get i array
155 then Native.Array.get i array
156 else default
156 else default
libraries/Dict.elm+20−20
@@ -1,5 +1,5 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2− ,get,getSafe,getUnsafe
2+ ,get,getOrElse,getOrFail
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, get, getMaybe, getWithDefault
22+@docs member, get, getOrElse, getOrFail
23
23
24# Combine
24# Combine
25@docs union, intersect, diff
25@docs union, intersect, diff
@@ -96,7 +96,7 @@ dictionary.
96 get "Mouse" animals == Just Mouse
96 get "Mouse" animals == Just Mouse
97 get "Spike" animals == Nothing
97 get "Spike" animals == Nothing
98
98
99−The `getSafe` and `getUnsafe` are built-in ways to handle common ways of
99+The `getOrElse` and `getOrFail` are built-in ways to handle common ways of
100using the resulting `Maybe`.
100using the resulting `Maybe`.
101-}
101-}
102get : comparable -> Dict comparable v -> Maybe v
102get : comparable -> Dict comparable v -> Maybe v
@@ -114,41 +114,41 @@ return a default value.
114
114
115 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
115 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
116
116
117− getSafe Dog "Tom" animals == Cat
117+ getOrElse Dog "Tom" animals == Cat
118− getSafe Dog "Mouse" animals == Mouse
118+ getOrElse Dog "Mouse" animals == Mouse
119− getSafe Dog "Spike" animals == Dog
119+ getOrElse Dog "Spike" animals == Dog
120-}
120-}
121−getSafe : v -> comparable -> Dict comparable v -> v
121+getOrElse : v -> comparable -> Dict comparable v -> v
122−getSafe base k t =
122+getOrElse base k t =
123 case t of
123 case t of
124 RBEmpty LBlack -> base
124 RBEmpty LBlack -> base
125 RBNode _ k' v l r ->
125 RBNode _ k' v l r ->
126 case Native.Utils.compare k k' of
126 case Native.Utils.compare k k' of
127− LT -> getSafe base k l
127+ LT -> getOrElse base k l
128 EQ -> v
128 EQ -> v
129− GT -> getSafe base k r
129+ GT -> getOrElse base k r
130
130
131{-| Get the value associated with a key.
131{-| Get the value associated with a key.
132
132
133 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
133 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134
134
135− get "Tom" animals == Cat
135+ getOrFail "Tom" animals == Cat
136− get "Mouse" animals == Mouse
136+ getOrFail "Mouse" animals == Mouse
137− get "Spike" animals == -- Runtime Error!
137+ getOrFail "Spike" animals == -- Runtime Error!
138
138
139Warning: this function will result in a runtime error if the key is not found,
139Warning: this function will result in a runtime error if the key is not found,
140−so it is best to use `get` or `getSafe` unless you are very confident the key
140+so it is best to use `get` or `getOrElse` unless you are sure the key will be
141−will be found.
141+found.
142-}
142-}
143−getUnsafe : comparable -> Dict comparable v -> v
143+getOrFail : comparable -> Dict comparable v -> v
144−getUnsafe k t =
144+getOrFail k t =
145 case t of
145 case t of
146− RBEmpty LBlack -> Native.Error.raise "key not found when using 'getUnsafe'"
146+ RBEmpty LBlack -> Native.Error.raise "key not found when using 'getOrFail'"
147 RBNode _ k' v l r ->
147 RBNode _ k' v l r ->
148 case Native.Utils.compare k k' of
148 case Native.Utils.compare k k' of
149− LT -> getUnsafe k l
149+ LT -> getOrFail k l
150 EQ -> v
150 EQ -> v
151− GT -> getUnsafe k r
151+ GT -> getOrFail k r
152
152
153{-| Determine if a key is in a dictionary. -}
153{-| Determine if a key is in a dictionary. -}
154member : comparable -> Dict comparable v -> Bool
154member : comparable -> Dict comparable v -> Bool
tests/elm/Test/Array.elm+3−3
@@ -30,9 +30,9 @@ tests =
30 [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
30 [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
31 , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
31 , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
32 , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
32 , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
33− , test "getSafe 1" <| assertEqual (Array.getSafe 0 2 (Array.fromList [3,2,1])) 1
33+ , test "getOrElse 1" <| assertEqual (Array.getOrElse 0 2 (Array.fromList [3,2,1])) 1
34− , test "getSafe 2" <| assertEqual (Array.getSafe 0 5 (Array.fromList [3,2,1])) 0
34+ , test "getOrElse 2" <| assertEqual (Array.getOrElse 0 5 (Array.fromList [3,2,1])) 0
35− , test "getUnsafe" <| assertEqual (Array.getUnsafe 2 (Array.fromList [3,2,1])) 1
35+ , test "getOrFail" <| assertEqual (Array.getOrFail 2 (Array.fromList [3,2,1])) 1
36 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
36 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
37 ]
37 ]
38 takingArraysApartTests =
38 takingArraysApartTests =
tests/elm/Test/Dict.elm+3−3
@@ -14,9 +14,9 @@ tests =
14 let getTests =
14 let getTests =
15 [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
15 [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
16 , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
16 , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
17− , test "getSafe 1" <| assertEqual "mouse" (Dict.getSafe "dog" "Jerry" animals)
17+ , test "getOrElse 1" <| assertEqual "mouse" (Dict.getOrElse "dog" "Jerry" animals)
18− , test "getSafe 2" <| assertEqual "dog" (Dict.getSafe "dog" "Spike" animals)
18+ , test "getOrElse 2" <| assertEqual "dog" (Dict.getOrElse "dog" "Spike" animals)
19− , test "getUnsafe" <| assertEqual "cat" (Dict.getUnsafe "Tom" animals)
19+ , test "getOrFail" <| assertEqual "cat" (Dict.getOrFail "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)