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.
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
response = {:ok, "hello world"}
elem(response, 1)
# => hello worldTuples are often used to represent grouped information
Float.ratio(0.25)
# => {1, 4} indicating the numerator and denominator of the fractionsources: