Skip to content

Commit 0f2838c

Browse files
author
José Valim
committed
Improve Enum docs
1 parent 8ae1a3b commit 0f2838c

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

lib/elixir/lib/enum.ex

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,26 @@ defmodule Enum do
5454

5555
@moduledoc """
5656
Provides a set of algorithms that enumerate over collections according to the
57-
`Enumerable` protocol. Most of the functions in this module have two
58-
flavours. If a given collection implements the mentioned protocol (like
59-
`List`, for instance), you can do:
57+
`Enumerable` protocol:
6058
61-
Enum.map([1, 2, 3], fn(x) -> x * 2 end)
59+
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end)
60+
[2,4,6]
61+
62+
Some particular types, like dictionaries, yield a specific format on
63+
enumeration. For dicts, the argument is always a `{ key, value }` tuple:
64+
65+
iex> dict = HashDict.new [a: 1, b: 2]
66+
iex> Enum.map(dict, fn { k, v } -> { k, v * 2 } end)
67+
[a: 2, b: 4]
68+
69+
Note that the `Enum` module is eager: it always evaluates the whole collection
70+
and returns a list out of it. The `Stream` module allows lazy enumeration of
71+
collections and also provides infinite streams. Infinite streams needs to be
72+
carefully used with `Enum` functions as they can potentially run forever, for
73+
example:
6274
63-
Depending on the type of the collection, the user-provided function will
64-
accept a certain type of argument. For dicts, the argument is always a
65-
`{ key, value }` tuple.
75+
Enum.each Stream.cycle([1,2,3]), &IO.puts(&1)
6676
67-
Note that all functions that return a collection return a list regardless of
68-
what the type of the input collection was and that many functions will not
69-
work with infinite streams.
7077
"""
7178

7279
@compile :inline_list_funcs

0 commit comments

Comments
 (0)