Editing
Tuple
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Tuple == A tuple is an ordered collection of elements that can be of different data types in the [[Elixir]] programming language. Tuples are immutable, meaning that once defined, their elements cannot be modified or removed. They provide a convenient way to store and manipulate related data. === Syntax === Tuples are defined using curly brackets ({}) and comma-separated values. The following is an example of a tuple: ``` {1, "Hello", :world} ``` Elements within a tuple can be accessed using the dot notation along with the index position. The index position starts from 0 for the first element. For example: ``` tuple = {1, "Hello", :world} IO.puts(tuple.1) # Output: 1 IO.puts(tuple.2) # Output: "Hello" IO.puts(tuple.3) # Output: :world ``` === Tuple Operations === Tuples support various operations in Elixir. Some of the commonly used operations include: ==== Size ==== The `Tuple.size/1` function returns the number of elements in a tuple. For example: ``` tuple = {1, "Hello", :world} IO.puts(Tuple.size(tuple)) # Output: 3 ``` ==== Concatenation ==== Tuples can be concatenated using the `++/2` operator. The resulting tuple contains all the elements of both tuples. For example: ``` tuple1 = {1, 2} tuple2 = {3, 4} concatenated = tuple1 ++ tuple2 IO.inspect(concatenated) # Output: {1, 2, 3, 4} ``` ==== Element Replacement ==== Although tuples are immutable, an element at a specific index can be replaced by creating a new tuple with desired changes. For example: ``` tuple = {1, 2, 3} replaced = put_elem(tuple, 1, "Hello") IO.inspect(replaced) # Output: {"Hello", 2, 3} ``` === Use Cases === Tuples are commonly used in Elixir for various purposes, including: * Returning multiple values from a function. * Representing coordinates or points in a 2D or 3D space. * Storing related configuration options. * Grouping related data together. Using tuples can provide a cleaner and more expressive codebase in certain scenarios. === Summary === Tuples in Elixir are immutable, ordered collections that allow storing different data types. They can be accessed using index notation and support operations like size calculation, concatenation, and element replacement. Tuples have several use cases, making them a valuable data structure in Elixir programming. == See Also == * [[List]] - An ordered collection of elements that can be dynamically modified. * [[Map]] - A key-value store for efficient data lookup and modification. * [[Keyword]] - An associative list with named keys for easy data retrieval. * [[Struct]] - A data structure for defining custom types with named fields.
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information