Back to Library
Region:
Switch to ID
Intermediate Exercism • elixir
Tuples
Lesson Overview
# Introduction
About
Tuples are used commonly to group information informally. A common pattern in Elixir is to group function return values with a status.
File.read("hello.txt")
# => {:ok, "World"}
File.read("invalid.txt")
# => {:error, :enoent}
- Tuple literals are enclosed with curly braces,
{}. - Tuples may hold any data type in contiguous memory, which is allocated when the tuple is created.
- Tuples allow for constant-time read-access.
- When manipulating a tuple, rather than mutating the existing tuple, a new one is created.
- The performance characteristics of tuples make them ideal for storing related information together, but not for storing collections of items that need iterating or might grow or shrink. In the latter case, a list is more appropriate.
- The
KernelandTuplemodules have useful functions for working with tuples.
Originally from Exercism elixir concepts