@@ -24,9 +24,10 @@ defmodule Dict do
2424 dictionaries are also required to implement the `Access`
2525 protocol:
2626
27- dict = HashDict.new
28- dict = Dict.put dict, :hello, :world
29- dict[:hello] #=> :world
27+ iex> dict = HashDict.new
28+ ...> dict = Dict.put dict, :hello, :world
29+ ...> dict[:hello]
30+ :world
3031
3132 And also the `Enum.Iterator` protocol, allowing one to write:
3233
@@ -77,8 +78,9 @@ defmodule Dict do
7778
7879 ## Examples
7980
80- d = new [a: 1, b: 2]
81- Dict.keys d #=> [:a,:b]
81+ iex> d = HashDict.new [a: 1, b: 2]
82+ ...> Enum.sort Dict.keys d
83+ [:a,:b]
8284
8385 """
8486 @ spec keys ( t ) :: [ key ]
@@ -91,8 +93,9 @@ defmodule Dict do
9193
9294 ## Examples
9395
94- d = new [a: 1, b: 2]
95- Dict.values d #=> [1,2]
96+ iex> d = HashDict.new [a: 1, b: 2]
97+ ...> Enum.sort Dict.values d
98+ [1,2]
9699
97100 """
98101 @spec values( t) :: [ value ]
@@ -105,8 +108,9 @@ defmodule Dict do
105108
106109 ## Examples
107110
108- d = new [a: 1, b: 2]
109- Dict.size d #=> 2
111+ iex> d = HashDict.new [a: 1, b: 2]
112+ ...> Dict.size d
113+ 2
110114
111115 """
112116 @spec size( t) :: non_neg_integer
@@ -119,9 +123,13 @@ defmodule Dict do
119123
120124 ## Examples
121125
122- d = new [a: 1]
123- Dict.has_key?(d, :a) #=> true
124- Dict.has_key?(d, :b) #=> false
126+ iex> d = HashDict.new [a: 1]
127+ ...> Dict.has_key?(d, :a)
128+ true
129+
130+ iex> d = HashDict.new [a: 1]
131+ ...> Dict.has_key?(d, :b)
132+ false
125133
126134 """
127135 @ spec has_key? ( t , key ) :: boolean
@@ -135,11 +143,17 @@ defmodule Dict do
135143
136144 ## Examples
137145
138- d = new [a: 1]
139- Dict.get d, :a #=> 1
140- Dict.get d, :b #=> nil
141- Dict.get d, :b, 3 #=> 3
146+ iex> d = HashDict.new [a: 1]
147+ ...> Dict.get d, :a
148+ 1
149+
150+ iex> d = HashDict.new [a: 1]
151+ ...> Dict.get d, :b
152+ nil
142153
154+ iex> d = HashDict.new [a: 1]
155+ ...> Dict.get d, :b, 3
156+ 3
143157 """
144158 @spec get( t, key , value ) :: value
145159 def get ( dict , key , default // nil ) do
@@ -152,9 +166,12 @@ defmodule Dict do
152166
153167 ## Examples
154168
155- d = new [a: 1]
156- Dict.get d, :a #=> 1
157- Dict.get d, :b #=> raises KeyError[key: :b]
169+ iex> d = HashDict.new [a: 1]
170+ ...> Dict.get d, :a
171+ 1
172+ iex> d = HashDict.new [a: 1]
173+ ...> Dict.get! d, :b
174+ ** (KeyError) key not found: :b
158175
159176 """
160177 @ spec get! ( t , key ) :: value | no_return
@@ -168,9 +185,10 @@ defmodule Dict do
168185
169186 ## Examples
170187
171- d = new [a: 1, b: 2]
172- Dict.put d, :a, 3
173- #=> [a: 3, b: 2]
188+ iex> d = HashDict.new [a: 1, b: 2]
189+ ...> d = Dict.put d, :a, 3
190+ ...> Dict.get d, :a
191+ 3
174192
175193 """
176194 @ spec put ( t , key , value ) :: t
@@ -183,9 +201,10 @@ defmodule Dict do
183201
184202 ## Examples
185203
186- d = new [a: 1, b: 2]
187- Dict.put_new d, :a, 3
188- #=> [a: 1, b: 2]
204+ iex> d = HashDict.new [a: 1, b: 2]
205+ ...> d = Dict.put_new d, :a, 3
206+ ...> Dict.get d, :a
207+ 1
189208
190209 """
191210 @ spec put_new ( t , key , value ) :: t
@@ -199,11 +218,14 @@ defmodule Dict do
199218
200219 ## Examples
201220
202- d = new [a: 1, b: 2]
203- Dict.delete d, :a #=> [b: 2]
221+ iex> d = HashDict.new [a: 1, b: 2]
222+ ...> d = Dict.delete d, :a
223+ ...> Dict.get d, :a
224+ nil
204225
205- d = new [b: 2]
206- Dict.delete d, :a #=> [b: 2]
226+ iex> d = HashDict.new [b: 2]
227+ ...> Dict.delete(d, :a) == d
228+ true
207229
208230 """
209231 @ spec delete ( t , key ) :: t
@@ -217,10 +239,11 @@ defmodule Dict do
217239
218240 ## Examples
219241
220- d1 = new [a: 1, b: 2]
221- d2 = new [a: 3, d: 4]
222- Dict.merge d1, d2
223- #=> [a: 3, b: 2, d: 4]
242+ iex> d1 = HashDict.new [a: 1, b: 2]
243+ ...> d2 = HashDict.new [a: 3, d: 4]
244+ ...> d = Dict.merge d1, d2
245+ ...> [a: Dict.get(d, :a), b: Dict.get(d, :b), d: Dict.get(d, :d)]
246+ [a: 3, b: 2, d: 4]
224247
225248 """
226249 @ spec merge ( t , t ) :: t
@@ -235,12 +258,13 @@ defmodule Dict do
235258
236259 ## Examples
237260
238- d1 = new [a: 1, b: 2]
239- d2 = new [a: 3, d: 4]
240- Dict.merge d1, d2, fn _k, v1, v2 ->
241- v1 + v2
242- end
243- #=> [a: 4, b: 2, d: 4]
261+ iex> d1 = HashDict.new [a: 1, b: 2]
262+ ...> d2 = HashDict.new [a: 3, d: 4]
263+ ...> d = Dict.merge d1, d2, fn _k, v1, v2 ->
264+ ...> v1 + v2
265+ ...> end
266+ ...> [a: Dict.get(d, :a), b: Dict.get(d, :b), d: Dict.get(d, :d)]
267+ [a: 4, b: 2, d: 4]
244268
245269 """
246270 @spec merge( t, t , ( key , value , value -> value ) ) :: t
@@ -254,9 +278,10 @@ defmodule Dict do
254278
255279 ## Examples
256280
257- d = new [a: 1, b: 2]
258- Dict.update d, :a, fn val -> -val end
259- #=> [a: -1, b: 2]
281+ iex> d = HashDict.new [a: 1, b: 2]
282+ ...> d = Dict.update d, :a, fn val -> -val end
283+ ...> Dict.get d, :a
284+ -1
260285
261286 """
262287 @spec update( t, key , ( value -> value ) ) :: t
@@ -271,9 +296,10 @@ defmodule Dict do
271296
272297 ## Examples
273298
274- d = new [a: 1, b: 2]
275- Dict.update d, :c, 3, fn val -> -val end
276- #=> [a: 1, b: 2, c: 3]
299+ iex> d = HashDict.new [a: 1, b: 2]
300+ ...> d = Dict.update d, :c, 3, fn val -> -val end
301+ ...> Dict.get d, :c
302+ 3
277303
278304 """
279305 @spec update( t, key , value , ( value -> value ) ) :: t
0 commit comments