We cover:

Chapter 10
Processing Collections—Enum and Stream

Elixir comes with a number of types that act as collections. We’ve already seen lists and maps. Ranges, files, and even functions can also act as collections. And as we’ll discuss when we look at protocols, you can also define your own.

Collections differ in their implementation. But they all share something: you can iterate through them. Some of them share an additional trait: you can add things to them.

Technically, things that can be iterated are said to implement the Enumerable protocol.

Elixir provides two modules that have a bunch of iteration functions. The Enum module is the workhorse for collections. You’ll use it all the time. I strongly recommend getting to know it.

The Stream module lets you enumerate a collection lazily. This means that the next value is calculated only when it is needed. You’ll use this less often, but when you do it’s a lifesaver.

I don’t want to fill this book with a list of all the APIs. You’ll find the definitive (and up-to-date) list online.[18] Instead, I’ll illustrate some common uses and let you browse the documentation for yourself. (But please do remember to do so. Much of Elixir’s power comes from these libraries.)