on master help sign in
Switch to the getOrElse/getOrFail name scheme

Also fix issue in which docs would be exported improperly

commit90f1956f68ac2f6abd0e2fd4de9199ca8e888d3e authorEvan Czaplicki <info@elm-lang.org> dateMon 28 Apr 2014, 07:46 (13 years ago) parentfe7619b
4 files changed+38−38
libraries/Array.elm+12−12 libraries/Dict.elm+20−20 tests/elm/Test/Array.elm+3−3 tests/elm/Test/Dict.elm+3−3
libraries/Array.elm+12−12
@@ -11,7 +11,7 @@ reads, updates, and appends.
1111@docs length, push, append
1212
1313# Get and Set
14−@docs get, getSafe, getUnsafe, set
14+@docs get, getOrElse, getOrFail, set
1515
1616# Taking Arrays Apart
1717@docs toList, toIndexedList, slice
@@ -122,14 +122,14 @@ push = Native.Array.push
122122
123123{-| Get the element at a particular index.
124124
125− getUnsafe 2 (A.fromList [3,2,1]) == 1
125+ getOrFail 2 (A.fromList [3,2,1]) == 1
126126
127127Warning: 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.
128+so it is best to use `get` or `getOrElse` unless you are sure the index will be
129+found.
130130-}
131−getUnsafe : Int -> Array a -> a
132−getUnsafe = Native.Array.get
131+getOrFail : Int -> Array a -> a
132+getOrFail = Native.Array.get
133133
134134{-| Return Just the element at the index or Nothing if the index is out of range.
135135
@@ -143,14 +143,14 @@ get i array =
143143 then Just (Native.Array.get i array)
144144 else Nothing
145145
146−{-| Get the element at the index. If the index is out of range, the given default
147−element is returned.
146+{-| Get the element at the index. Or if the index is out of range, a default
147+value is returned.
148148
149− getSafe 0 2 (fromList [3,2,1]) == 1
150− getSafe 0 5 (fromList [3,2,1]) == 0
149+ getOrElse 0 2 (fromList [3,2,1]) == 1
150+ getOrElse 0 5 (fromList [3,2,1]) == 0
151151-}
152−getSafe : a -> Int -> Array a -> a
153−getSafe default i array =
152+getOrElse : a -> Int -> Array a -> a
153+getOrElse default i array =
154154 if 0 <= i && i < Native.Array.length array
155155 then Native.Array.get i array
156156 else default
libraries/Dict.elm+20−20
@@ -1,5 +1,5 @@
11module Dict (empty,singleton,insert,update
2− ,get,getSafe,getUnsafe
2+ ,get,getOrElse,getOrFail
33 ,remove,member
44 ,filter
55 ,partition
@@ -19,7 +19,7 @@ Insert, remove, and query operations all take *O(log n)* time.
1919@docs empty, singleton, insert, update, remove
2020
2121# Query
22−@docs member, get, getMaybe, getWithDefault
22+@docs member, get, getOrElse, getOrFail
2323
2424# Combine
2525@docs union, intersect, diff
@@ -96,7 +96,7 @@ dictionary.
9696 get "Mouse" animals == Just Mouse
9797 get "Spike" animals == Nothing
9898
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
100100using the resulting `Maybe`.
101101-}
102102get : comparable -> Dict comparable v -> Maybe v
@@ -114,41 +114,41 @@ return a default value.
114114
115115 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
116116
117− getSafe Dog "Tom" animals == Cat
118− getSafe Dog "Mouse" animals == Mouse
119− getSafe Dog "Spike" animals == Dog
117+ getOrElse Dog "Tom" animals == Cat
118+ getOrElse Dog "Mouse" animals == Mouse
119+ getOrElse Dog "Spike" animals == Dog
120120-}
121−getSafe : v -> comparable -> Dict comparable v -> v
122−getSafe base k t =
121+getOrElse : v -> comparable -> Dict comparable v -> v
122+getOrElse base k t =
123123 case t of
124124 RBEmpty LBlack -> base
125125 RBNode _ k' v l r ->
126126 case Native.Utils.compare k k' of
127− LT -> getSafe base k l
127+ LT -> getOrElse base k l
128128 EQ -> v
129− GT -> getSafe base k r
129+ GT -> getOrElse base k r
130130
131131{-| Get the value associated with a key.
132132
133133 animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134134
135− get "Tom" animals == Cat
136− get "Mouse" animals == Mouse
137− get "Spike" animals == -- Runtime Error!
135+ getOrFail "Tom" animals == Cat
136+ getOrFail "Mouse" animals == Mouse
137+ getOrFail "Spike" animals == -- Runtime Error!
138138
139139Warning: 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
141−will be found.
140+so it is best to use `get` or `getOrElse` unless you are sure the key will be
141+found.
142142-}
143−getUnsafe : comparable -> Dict comparable v -> v
144−getUnsafe k t =
143+getOrFail : comparable -> Dict comparable v -> v
144+getOrFail k t =
145145 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'"
147147 RBNode _ k' v l r ->
148148 case Native.Utils.compare k k' of
149− LT -> getUnsafe k l
149+ LT -> getOrFail k l
150150 EQ -> v
151− GT -> getUnsafe k r
151+ GT -> getOrFail k r
152152
153153{-| Determine if a key is in a dictionary. -}
154154member : comparable -> Dict comparable v -> Bool
tests/elm/Test/Array.elm+3−3
@@ -30,9 +30,9 @@ tests =
3030 [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
3131 , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
3232 , 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
34− , test "getSafe 2" <| assertEqual (Array.getSafe 0 5 (Array.fromList [3,2,1])) 0
35− , test "getUnsafe" <| assertEqual (Array.getUnsafe 2 (Array.fromList [3,2,1])) 1
33+ , test "getOrElse 1" <| assertEqual (Array.getOrElse 0 2 (Array.fromList [3,2,1])) 1
34+ , test "getOrElse 2" <| assertEqual (Array.getOrElse 0 5 (Array.fromList [3,2,1])) 0
35+ , test "getOrFail" <| assertEqual (Array.getOrFail 2 (Array.fromList [3,2,1])) 1
3636 , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
3737 ]
3838 takingArraysApartTests =
tests/elm/Test/Dict.elm+3−3
@@ -14,9 +14,9 @@ tests =
1414 let getTests =
1515 [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
1616 , 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)
17+ , test "getOrElse 1" <| assertEqual "mouse" (Dict.getOrElse "dog" "Jerry" animals)
18+ , test "getOrElse 2" <| assertEqual "dog" (Dict.getOrElse "dog" "Spike" animals)
19+ , test "getOrFail" <| assertEqual "cat" (Dict.getOrFail "Tom" animals)
2020 ]
2121 filterTests =
2222 [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)