Book Image

Elixir Cookbook

By : Paulo Pereira
Book Image

Elixir Cookbook

By: Paulo Pereira

Overview of this book

Table of Contents (16 chapters)
Elixir Cookbook
About the Author
About the Reviewers

Combining tuples into a list

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.

Getting ready

In this recipe, we will be using an IEx session. Start it by executing iex in your console.

How to do it…

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:

  1. Create tuple_one:

    iex(1)> tuple_one = {:one, :two, :three}
    {:one, :two, :three}
  2. Create tuple_two:

    iex(2)> tuple_two = {1, 2, 3, 4}
    {1, 2, 3, 4}
  3. Try to interpolate these two tuples by combining each value on the nth position of tuple_one with the nth value of tuple_two. We will be using the function:

    ** (Protocol.UndefinedError) protocol Enumerable not implemented for {:one...