Collectable

Learn and implement the Collectable built-in protocol.

We'll cover the following...

Introduction

We’ve already seen Enum.into. It takes something that’s enumerable and creates a new collection from it:

iex> 1..4 |> Enum.into([])
[1, 2, 3, 4]
iex> [ {1, 2}, {"a", "b"}] |> Enum.into(%{}) 
%{1 => 2, "a" => "b"}

The target of Enum.into must implement the Collectable protocol. This defines a single function, somewhat confusingly also called into. This function returns a two-element tuple. The first element is the initial value of the target collection. The second is a function to be called to add ...