# Enum 模块

Some contents of this translation may be outdated.
Several minor changes were applied to the original lesson since the last update.

## Enum

`Enum` 模块提供了超过一百个函数，和我们上节课提到的集合交互。

``````iex
iex> Enum.__info__(:functions) |> Enum.each(fn({function, arity}) ->
...>   IO.puts "#{function}/#{arity}"
...> end)
all?/1
all?/2
any?/1
any?/2
at/2
at/3
...
``````

### all?

``````iex> Enum.all?(["foo", "bar", "hello"], fn(s) -> String.length(s) == 3 end)
false
iex> Enum.all?(["foo", "bar", "hello"], fn(s) -> String.length(s) > 1 end)
true
``````

### any?

``````iex> Enum.any?(["foo", "bar", "hello"], fn(s) -> String.length(s) == 5 end)
true
``````

### chunk_every/2

``````iex> Enum.chunk_every([1, 2, 3, 4, 5, 6], 2)
[[1, 2], [3, 4], [5, 6]]
``````

`chunk_every/2` 还有其他选项，在这里不深入介绍。如果感兴趣，前往 `chunk_every/4` 的官方文档去了解。

### chunk_by

``````iex> Enum.chunk_by(["one", "two", "three", "four", "five"], fn(x) -> String.length(x) end)
[["one", "two"], ["three"], ["four", "five"]]
iex> Enum.chunk_by(["one", "two", "three", "four", "five", "six"], fn(x) -> String.length(x) end)
[["one", "two"], ["three"], ["four", "five"], ["six"]]
``````

### map_every

``````iex> Enum.map_every([1, 2, 3, 4], 2, fn x -> x * 2 end)
[2, 2, 6, 4]
``````

### each

``````iex> Enum.each(["one", "two", "three"], fn(s) -> IO.puts(s) end)
one
two
three
:ok
``````

### map

``````iex> Enum.map([0, 1, 2, 3], fn(x) -> x - 1 end)
[-1, 0, 1, 2]
``````

### min

`min/1` 在集合中找到最小的值：

``````iex> Enum.min([5, 3, 0, -1])
-1
``````

`min/2` 也一样，但是它允许我们提供一个匿名函数指定计算最小值的方法：

``````iex> Enum.min([], fn -> :foo end)
:foo
``````

### max

`max/1` 返回集合中最大的值：

``````iex> Enum.max([5, 3, 0, -1])
5
``````

`max/2` 也一样，而且像 `min/2` 一样，它允许我们提供一个匿名函数指定计算最大值的方法：

``````iex> Enum.max([], fn -> :bar end)
:bar
``````

### reduce

``````iex> Enum.reduce([1, 2, 3], 10, fn(x, acc) -> x + acc end)
16
iex> Enum.reduce([1, 2, 3], fn(x, acc) -> x + acc end)
6
iex> Enum.reduce(["a","b","c"], "1", fn(x,acc)-> x <> acc end)
"cba1"
``````

### sort

``````iex> Enum.sort([5, 6, 1, 3, -1, 4])
[-1, 1, 3, 4, 5, 6]

iex> Enum.sort([:foo, "bar", Enum, -1, 4])
[-1, 4, Enum, :foo, "bar"]
``````

``````# with our function
iex> Enum.sort([%{:val => 4}, %{:val => 1}], fn(x, y) -> x[:val] > y[:val] end)
[%{val: 4}, %{val: 1}]

# without
iex> Enum.sort([%{:count => 4}, %{:count => 1}])
[%{count: 1}, %{count: 4}]
``````

### uniq_by

``````iex> Enum.uniq_by([1, 2, 3, 2, 1, 1, 1, 1, 1], fn x -> x end)
[1, 2, 3]
``````