concept tuple in category elixir

appears as: Tuples, tuple, tuple, A tuple, The tuple
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

Tuples are something like untyped structures, or records, and they’re most often used to group a fixed number of elements together. The following snippet defines a tuple consisting of a person’s name and age:

iex(1)> person = {"Bob", 25}
{"Bob", 25}

To extract an element from the tuple, you can use the Kernel.elem/2 function, which accepts a tuple and the zero-based index of the element. Recall that the Kernel module is auto-imported, so you can call elem instead of Kernel.elem:

iex(2)> age = elem(person, 1)
25

To modify an element of the tuple, you can use the Kernel.put_elem/3 function, which accepts a tuple, a zero-based index, and the new value of the field in the given position:

iex(3)> put_elem(person, 1, 26)
{"Bob", 26}

The function put_elem doesn’t modify the tuple. It returns the new version, keeping the old one intact. Recall that data in Elixir is immutable, so you can’t do an in-memory modification of a value. You can verify that the previous call to put_elem didn’t change the person variable:

Let’s start with tuples. A modified tuple is always a complete, shallow copy of the old version. Consider the following code:

a_tuple = {a, b, c}
new_tuple = put_elem(a_tuple, 1, b2)

Figure 2.2 Modifying a tuple creates a shallow copy of it.

modifying_tuples.png
The Little Elixir & OTP Guidebook

This is an excerpt from Manning's book The Little Elixir & OTP Guidebook.

Listing 2.4. Tic-tac-toe board that uses tuples to represent board configurations
def check_board(board) do
  case board do
    { :x, :x, :x,
      _ , _ , _ ,
      _ , _ , _ } -> :x_win
    { _ , _ , _ ,
      :x, :x, :x,
      _ , _ , _ } -> :x_win

    { _ , _ , _ ,
      _ , _ , _ ,
      :x, :x, :x} -> :x_win

    { :x, _ , _ ,
      :x, _ , _ ,
      :x, _ , _ } -> :x_win

    { _ , :x, _ ,
      _ , :x, _ ,
      _ , :x, _ } -> :x_win

    { _ , _ , :x,
      _ , _ , :x,
      _ , _ , :x} -> :x_win

    { :x, _ , _ ,
      _ , :x, _ ,
      _ , _ , :x} -> :x_win

    { _ , _ , :x,
      _ , :x, _ ,
      :x, _ , _ } -> :x_win

    # Player O board patterns omitted ...

    { a, b, c,
      d, e, f,
      g, h, i } when a and b and c and d and e and f and g and h and i -> :draw

    _ -> :in_progress

  end
end

A tuple with the atom :ok and the value (the programming language) is returned when a key is found, or an :error atom otherwise. You can see how tuples and atoms are useful and how you can exploit this with pattern matching. By using the return values of both the happy ({:ok, language}) and exceptional paths (:error), you can express yourself as follows:

You expect a synchronous reply from the server. Therefore, you should invoke GenServer.call/3. Here, you’re saying that the server should handle a synchronous :get_stats message. Notice that messages can come in the form of any valid Elixir term. This means tuples, lists, and atoms are all fair game. The next listing shows the callback function.

Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

Figure 2.2. Modifying a tuple creates a shallow copy of it.

Tuples are something like untyped structures, or records, and they’re most often used to group a fixed number of elements together. The following snippet defines a tuple consisting of person’s name and age:

iex(1)> person = {"Bob", 25}
{"Bob", 25}

To extract an element from the tuple, you can use the Kernel.elem/2 function, which accepts a tuple and the zero-based index of the element. Recall that the Kernel module is auto-imported, so you can call elem instead of Kernel.elem:

iex(2)> age = elem(person, 1)
25

To modify an element of the tuple, you can use the Kernel.put_elem/3 function, which accepts a tuple, a zero-based index, and the new value of the field in the given position:

iex(3)> put_elem(person, 1, 26)
{"Bob", 26}

The function put_elem doesn’t modify the tuple. It returns the new version, keeping the old one intact. Recall that data in Elixir is immutable, and you can’t do an in-memory modification of a value. You can verify that the previous call to put_elem didn’t change the person variable:

Pattern matching is an important construct in Elixir. It’s a feature that makes manipulations with complex variables (such as tuples and lists) a lot easier. Less obviously, it allows you to write elegant, declarative-like conditionals and loops. You’ll see what this means by the end of the chapter; in this section, we’ll look at the basic mechanical workings of pattern matching. Let’s begin by looking at the match operator.

3.1.2. Matching tuples

The following example demonstrates basic pattern matching of tuples:

iex(1)> {name, age} = {"Bob", 25}

This expression assumes that the right-side term is a tuple of two elements. When the expression is evaluated, the variables name and age are bound to the corresponding elements of the tuple. You can now verify that these variables are correctly bound:

iex(2)> name
"Bob"

iex(3)> age
25

This feature is useful when you call a function that returns a tuple and you want to bind individual elements of that tuple to separate variables. The following example calls the Erlang function :calendar.local_time/0 to get the current date and time:

sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest