Skip to content

Commit 5b9c3e7

Browse files
committed
Make Enum.Iterator's docstring more descriptive
1 parent c125636 commit 5b9c3e7

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lib/elixir/lib/enum.ex

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,55 @@
11
defprotocol Enum.Iterator do
22
@moduledoc """
33
This is the protocol used by the `Enum` module.
4-
Usually, when you invoke a function in the module `Enum`,
5-
the first argument passed to `Enum` is a collection which
6-
is forwarded to this protocol in order to retrieve information
7-
on how to iterate the collection. That said, when:
4+
5+
Usually, when you invoke a function in the module `Enum`, the first argument
6+
passed to it is a collection which is forwarded to this protocol in order to
7+
retrieve information on how to iterate the collection.
8+
9+
For example, in the expression
810
911
Enum.map [1,2,3], &1 * 2
1012
11-
Is invoked, it invokes `Enum.Iterator.iterator([1,2,3])`
12-
which returns all the information required by Enum.
13-
Read each function documentation below for more information.
13+
`Enum.map` invokes `Enum.Iterator.iterator([1,2,3])` to retrieve the iterator
14+
function that will drive the iteration process.
1415
"""
1516

1617
@only [List, Record, Function]
1718

1819
@doc """
19-
Iteration in Elixir happens with the help of a iterator
20-
function. Every time this function is called, it must
21-
return a tuple with two elements. The first element
22-
is the next item and the second can be any Elixir term
23-
which the function is going to receive as argument the
24-
next time it is invoked.
20+
This function must return a tuple of the form `{ iter, step }` where
21+
`iter` is a function that yields successive values from the collection
22+
each time it is invoked and `step` is the first step of iteration.
2523
26-
When there are no more items to be iterated, the function
27-
must return the atom `:stop`.
24+
Iteration in Elixir happens with the help of an _iterator function_ (named
25+
`iter` in the paragraph above). When it is invoked, it must return a tuple
26+
with two elements. The first element is the next successive value from the
27+
collection and the second element can be any Elixir term which `iter` is
28+
going to receive as its argument the next time it is invoked.
2829
29-
In order to retrieve this iterator function, Elixir invokes
30-
`Enum.Iterator.iterator(collection)` which should return a
31-
tuple with two elements: the first element is the iterator
32-
function and the second is the first step of iteration.
30+
When there are no more items left to yield, `iter` must return the atom
31+
`:stop`.
3332
34-
As an example, here is the implementation of iterator for lists:
33+
As an example, here is the implementation of `iterator` for lists:
3534
3635
def iterator(list), do: { iterate(&1), iterate(list) }
3736
defp iterate([h|t]), do: { h, t }
3837
defp iterate([]), do: :stop
3938
39+
Here, `iterate` is the _iterator function_ and `{ h, t }` is a step of
40+
iteration.
41+
4042
## Iterating lists
4143
42-
If a data structure needs to be converted to a list in order
43-
to be iterated, the iterator function can simply return the
44-
list and the Enum module will be able to take over the list
45-
and retrieve the proper iterator function.
44+
As a special case, if a data structure needs to be converted to a list in
45+
order to be iterated, `iterator` can simply return the list and the `Enum`
46+
module will be able to take over the list and produce a proper iterator
47+
function for it.
4648
"""
4749
def iterator(collection)
4850

4951
@doc """
50-
The function used to retrieve the collection size.
52+
The function used to retrieve the collection's size.
5153
"""
5254
def count(collection)
5355
end

0 commit comments

Comments
 (0)