Elixir has a tuple data type. A tuple, like a list, can contain different types at the same time but guarantees that its elements are stored contiguously in memory.
Tuples are declared using brackets ({}
) and are often used as function return values and in-function pattern matching.
In this recipe, we will be using an IEx session. Start it by executing iex
in your console.
We will create two tuples, one with atoms and one with integers, and then we will combine them. To do so, we need to convert them into lists:
Create
tuple_one
:iex(1)> tuple_one = {:one, :two, :three} {:one, :two, :three}
Create
tuple_two
:iex(2)> tuple_two = {1, 2, 3, 4} {1, 2, 3, 4}
Try to interpolate these two tuples by combining each value on the nth position of
tuple_one
with the nth value oftuple_two
. We will be using theEnum.zip/2
function:iex(3)> Enum.zip(tuple_one,tuple_two) ** (Protocol.UndefinedError) protocol Enumerable not implemented for {:one...