tuple (ex_stdlib v0.2.0)

View Source

Tuple operations.

This module provides functions for working with tuples, including creation, modification, and conversion operations. Tuples are intended as fixed-size containers for multiple elements with constant-time access but linear-time modification.

Note: The following functions for tuples are found in Erlang BIFs: - element/2 - accesses a tuple by index (1-based) - setelement/3 - updates a value in a tuple by index (1-based) - tuple_size/1 - gets the number of elements in a tuple

Examples:

   % Create a tuple with duplicated elements
   {hello, hello, hello} = tuple:duplicate(hello, 3),
  
   % Insert element at specific position (0-based indexing)
   {foo, bar, baz} = tuple:insert_at({bar, baz}, 0, foo),
  
   % Convert tuple to list
   [foo, bar, baz] = tuple:to_list({foo, bar, baz}),
  
   % Compute sum and product
   6 = tuple:sum({1, 2, 3}),
   24 = tuple:product({2, 3, 4}).

Summary

Functions

Tests whether all elements of a tuple satisfy a predicate.

Tests whether any element of a tuple satisfies a predicate.

Appends an element to the end of a tuple.

Removes an element from a tuple.

Creates a new tuple.

Gets an element from a tuple by index.

Filters elements of a tuple based on a predicate.

Converts a list to a tuple.

Inserts an element into a tuple.

Maps a function over all elements of a tuple.

Computes the product of tuple elements.

Puts an element in a tuple by index.

Reverses the elements of a tuple.

Gets the size of a tuple.

Computes the sum of tuple elements.

Converts a tuple to a list.

Unzips a tuple of pairs into two tuples.

Zips two tuples into a tuple of pairs.

Types

tuple_index/0

-type tuple_index() :: non_neg_integer().

Functions

all(Fun, Tuple)

-spec all(fun((term()) -> boolean()), tuple()) -> boolean().

Tests whether all elements of a tuple satisfy a predicate.

Returns true if Fun returns true for all elements, false otherwise. Returns true for an empty tuple.

Examples:

   true = tuple:all(fun(X) -> X > 0 end, {1, 2, 3}),
   false = tuple:all(fun(X) -> X > 2 end, {1, 2, 3}),
   true = tuple:all(fun(_) -> false end, {}).

any(Fun, Tuple)

-spec any(fun((term()) -> boolean()), tuple()) -> boolean().

Tests whether any element of a tuple satisfies a predicate.

Returns true if Fun returns true for at least one element, false otherwise. Returns false for an empty tuple.

Examples:

   true = tuple:any(fun(X) -> X > 2 end, {1, 2, 3}),
   false = tuple:any(fun(X) -> X > 5 end, {1, 2, 3}),
   false = tuple:any(fun(_) -> true end, {}).

append(Tuple, Value)

-spec append(tuple(), term()) -> tuple().

Appends an element to the end of a tuple.

Adds Value as the last element of Tuple, creating a new tuple. Equivalent to insert_at(Tuple, tuple_size(Tuple), Value).

Examples:

   {foo, bar, baz} = tuple:append({foo, bar}, baz),
   {hello} = tuple:append({}, hello).

delete_at(Tuple, Index)

-spec delete_at(tuple(), tuple_index()) -> tuple().

Removes an element from a tuple.

Deletes the element at the given Index (0-based) from Tuple. Raises badarg if Index is negative or greater than or equal to the length of Tuple.

Examples:

   {bar, baz} = tuple:delete_at({foo, bar, baz}, 0),
   {foo, baz} = tuple:delete_at({foo, bar, baz}, 1).

duplicate(Data, Size)

-spec duplicate(term(), non_neg_integer()) -> tuple().

Creates a new tuple.

Creates a tuple of Size containing the given Data at every position. Equivalent to Erlang's make_tuple/2.

Examples:

   {hello, hello, hello} = tuple:duplicate(hello, 3),
   {} = tuple:duplicate(anything, 0),
   {42} = tuple:duplicate(42, 1).

elem(Tuple, Index)

-spec elem(tuple(), tuple_index()) -> term().

Gets an element from a tuple by index.

Returns the element at the given Index (0-based) in Tuple. Raises badarg if Index is out of bounds.

Examples:

   foo = tuple:elem({foo, bar, baz}, 0),
   baz = tuple:elem({foo, bar, baz}, 2).

filter(Fun, Tuple)

-spec filter(fun((term()) -> boolean()), tuple()) -> tuple().

Filters elements of a tuple based on a predicate.

Returns a new tuple containing only elements for which Fun returns true.

Examples:

   {2, 4} = tuple:filter(fun(X) -> X rem 2 =:= 0 end, {1, 2, 3, 4}),
   {} = tuple:filter(fun(_) -> false end, {1, 2, 3}).

from_list(List)

-spec from_list(list()) -> tuple().

Converts a list to a tuple.

Returns a new tuple with all the list elements in the same order. Equivalent to Erlang's list_to_tuple/1.

Examples:

   {foo, bar, baz} = tuple:from_list([foo, bar, baz]),
   {} = tuple:from_list([]).

insert_at(Tuple, Index, Value)

-spec insert_at(tuple(), tuple_index(), term()) -> tuple().

Inserts an element into a tuple.

Inserts Value into Tuple at the given Index (0-based). Raises badarg if Index is negative or greater than the length of Tuple.

Examples:

   {foo, bar, baz} = tuple:insert_at({bar, baz}, 0, foo),
   {bar, baz, bong} = tuple:insert_at({bar, baz}, 2, bong).

map(Fun, Tuple)

-spec map(fun((term()) -> term()), tuple()) -> tuple().

Maps a function over all elements of a tuple.

Applies Fun to each element of Tuple and returns a new tuple with the results.

Examples:

   {2, 4, 6} = tuple:map(fun(X) -> X * 2 end, {1, 2, 3}),
   {} = tuple:map(fun(X) -> X end, {}).

product(Tuple)

-spec product(tuple()) -> number().

Computes the product of tuple elements.

Returns the product of all numeric elements in the tuple. Returns 1 for an empty tuple.

Examples:

   65025 = tuple:product({255, 255}),
   255.0 = tuple:product({255, 1.0}),
   1 = tuple:product({}).

put_elem(Tuple, Index, Value)

-spec put_elem(tuple(), tuple_index(), term()) -> tuple().

Puts an element in a tuple by index.

Returns a new tuple with Value at the given Index (0-based). Raises badarg if Index is out of bounds.

Examples:

   {hello, bar, baz} = tuple:put_elem({foo, bar, baz}, 0, hello),
   {foo, bar, world} = tuple:put_elem({foo, bar, baz}, 2, world).

reverse(Tuple)

-spec reverse(tuple()) -> tuple().

Reverses the elements of a tuple.

Returns a new tuple with elements in reverse order.

Examples:

   {c, b, a} = tuple:reverse({a, b, c}),
   {} = tuple:reverse({}).

size(Tuple)

-spec size(tuple()) -> non_neg_integer().

Gets the size of a tuple.

Returns the number of elements in Tuple. Equivalent to Erlang's tuple_size/1.

Examples:

   3 = tuple:size({foo, bar, baz}),
   0 = tuple:size({}).

sum(Tuple)

-spec sum(tuple()) -> number().

Computes the sum of tuple elements.

Returns the sum of all numeric elements in the tuple. Returns 0 for an empty tuple.

Examples:

   510 = tuple:sum({255, 255}),
   255.0 = tuple:sum({255, 0.0}),
   0 = tuple:sum({}).

to_list(Tuple)

-spec to_list(tuple()) -> list().

Converts a tuple to a list.

Returns a new list with all the tuple elements in the same order. Equivalent to Erlang's tuple_to_list/1.

Examples:

   [foo, bar, baz] = tuple:to_list({foo, bar, baz}),
   [] = tuple:to_list({}).

unzip(Tuple)

-spec unzip(tuple()) -> {tuple(), tuple()}.

Unzips a tuple of pairs into two tuples.

Separates a tuple of pairs into two tuples.

Examples:

   {{a, b, c}, {1, 2, 3}} = tuple:unzip({{a, 1}, {b, 2}, {c, 3}}),
   {{}, {}} = tuple:unzip({}).

zip(Tuple1, Tuple2)

-spec zip(tuple(), tuple()) -> tuple().

Zips two tuples into a tuple of pairs.

Combines two tuples element-wise into pairs. If the tuples have different sizes, the result will have the size of the smaller tuple.

Examples:

   {{a, 1}, {b, 2}, {c, 3}} = tuple:zip({a, b, c}, {1, 2, 3}),
   {{a, 1}, {b, 2}} = tuple:zip({a, b}, {1, 2, 3}).