O'Reilly logo

Elixir Cookbook by Paulo A Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required