@@ -85,6 +85,7 @@ defmodule Enum do
85
85
@ type t :: Enum.Iterator . t
86
86
@ type element :: any
87
87
@ type index :: non_neg_integer
88
+ @ type default :: any
88
89
89
90
@ doc """
90
91
Returns true if the `collection` is empty, otherwise false.
@@ -233,6 +234,34 @@ defmodule Enum do
233
234
Raises out of bounds error in case the given position
234
235
is outside the range of the collection.
235
236
237
+ Expects an ordered collection.
238
+
239
+ ## Examples
240
+
241
+ iex> Enum.at([2,4,6], 0)
242
+ 2
243
+ iex> Enum.at([2,4,6], 2)
244
+ 6
245
+ iex> Enum.at([2,4,6], 4)
246
+ nil
247
+ iex> Enum.at([2,4,6], 4, :none)
248
+ :none
249
+
250
+ """
251
+ @ spec at ( t , index ) :: element | nil
252
+ @ spec at ( t , index , default ) :: element | default
253
+ def at ( collection , n , default // nil ) when n >= 0 do
254
+ case fetch ( collection , n ) do
255
+ { :ok , h } -> h
256
+ :error -> default
257
+ end
258
+ end
259
+
260
+ @ doc """
261
+ Finds the element at the given index (zero-based).
262
+ Raises out of bounds error in case the given position
263
+ is outside the range of the collection.
264
+
236
265
Expects an ordered collection.
237
266
238
267
## Examples
@@ -246,16 +275,10 @@ defmodule Enum do
246
275
247
276
"""
248
277
@ spec at! ( t , index ) :: element | no_return
249
- def at! ( collection , n ) when is_list ( collection ) and n >= 0 do
250
- do_at! ( collection , n )
251
- end
252
-
253
278
def at! ( collection , n ) when n >= 0 do
254
- case I . iterator ( collection ) do
255
- { iterator , pointer } ->
256
- do_at! ( pointer , iterator , n )
257
- list when is_list ( list ) ->
258
- do_at! ( list , n )
279
+ case fetch ( collection , n ) do
280
+ { :ok , h } -> h
281
+ :error -> raise Enum.OutOfBoundsError
259
282
end
260
283
end
261
284
@@ -468,6 +491,36 @@ defmodule Enum do
468
491
end
469
492
end
470
493
494
+ @ doc """
495
+ Finds the element at the given index (zero-based).
496
+ Returns `{ :ok, element }` if found, otherwise `:error`.
497
+
498
+ Expects an ordered collection.
499
+
500
+ ## Examples
501
+
502
+ iex> Enum.fetch([2,4,6], 0)
503
+ { :ok, 2 }
504
+ iex> Enum.fetch([2,4,6], 2)
505
+ { :ok, 6 }
506
+ iex> Enum.fetch([2,4,6], 4)
507
+ :error
508
+
509
+ """
510
+ @ spec fetch ( t , index ) :: { :ok , element } | :error
511
+ def fetch ( collection , n ) when is_list ( collection ) and n >= 0 do
512
+ do_fetch ( collection , n )
513
+ end
514
+
515
+ def fetch ( collection , n ) when n >= 0 do
516
+ case I . iterator ( collection ) do
517
+ { iterator , pointer } ->
518
+ do_fetch ( pointer , iterator , n )
519
+ list when is_list ( list ) ->
520
+ do_fetch ( list , n )
521
+ end
522
+ end
523
+
471
524
@ doc """
472
525
Filters the collection, i.e. returns only those elements
473
526
for which `fun` returns true.
@@ -531,8 +584,8 @@ defmodule Enum do
531
584
3
532
585
533
586
"""
534
- @ spec find ( t , ( element -> any ) ) :: element | : nil
535
- @ spec find ( t , any , ( element -> any ) ) :: element | :nil
587
+ @ spec find ( t , ( element -> any ) ) :: element | nil
588
+ @ spec find ( t , default , ( element -> any ) ) :: element | default
536
589
537
590
def find ( collection , ifnone // nil , fun )
538
591
@@ -1277,15 +1330,15 @@ defmodule Enum do
1277
1330
false
1278
1331
end
1279
1332
1280
- ## at!
1333
+ ## fetch
1281
1334
1282
- defp do_at! ( [ h | _ ] , 0 ) , do: h
1283
- defp do_at! ( [ _ | t ] , n ) , do: do_at! ( t , n - 1 )
1284
- defp do_at! ( [ ] , _ ) , do: raise Enum.OutOfBoundsError
1335
+ defp do_fetch ( [ h | _ ] , 0 ) , do: { :ok , h }
1336
+ defp do_fetch ( [ _ | t ] , n ) , do: do_fetch ( t , n - 1 )
1337
+ defp do_fetch ( [ ] , _ ) , do: :error
1285
1338
1286
- defp do_at! ( { h , _next } , _iterator , 0 ) , do: h
1287
- defp do_at! ( { _ , next } , iterator , n ) , do: do_at! ( iterator . ( next ) , iterator , n - 1 )
1288
- defp do_at! ( :stop , _iterator , _ ) , do: raise Enum.OutOfBoundsError
1339
+ defp do_fetch ( { h , _next } , _iterator , 0 ) , do: { :ok , h }
1340
+ defp do_fetch ( { _ , next } , iterator , n ) , do: do_fetch ( iterator . ( next ) , iterator , n - 1 )
1341
+ defp do_fetch ( :stop , _iterator , _ ) , do: :error
1289
1342
1290
1343
## count
1291
1344
0 commit comments