links: [[Elixir MOC]] --- Tuple is a data structure which organizes data, holding a fixed number of items of any type, but without explicit names for each element. Tuples are often used in Elixir for memory read-intensive operations since read-access of an element is a constant time operation. Tuples are created using curly braces. ```elixir empty_tuple = {} one_element_tuple = {1} multi_element_tuple = {1, :a, "hello"} ``` Tuples are stored contiguously in memory, that's why accessing a tuple by index or getting the size of a tuple is a fast operation. Elements in tuples can be accessed individually using `elem/2` function using 0-based indexing ```elixir response = {:ok, "hello world"} elem(response, 1) # => hello world ``` Tuples are often used to represent grouped information ```elixir Float.ratio(0.25) # => {1, 4} indicating the numerator and denominator of the fraction ``` --- tags: #elixir #tuple sources: - [Getting started with elixir: Tuples](https://elixir-lang.org/getting-started/basic-types.html#tuples) - [Lists vs Tuples](https://blog.appsignal.com/2018/08/21/elixir-alchemy-list-vs-tuples.html) - [Tuple hexdocs](https://hexdocs.pm/elixir/Tuple.html)