Tuples in Julia

Tuples in Julia are an immutable collection of distinct values of same or different datatypes separated by commas. Tuples are more like arrays in Julia except that arrays only take values of similar datatypes. The values of a tuple can not be changed because tuples are immutable. Tuples are a heterogeneous collection of values.

The sequence of values stored in a tuple can be of any type, and they are indexed by integers.
Values of a tuple are syntactically separated by ‘commas’. Although it is not necessary, it is more common to define a tuple by closing the sequence of values in parentheses. This helps in understanding the Julia tuples more easily.

Syntax:

Tuple_name = (value1, value2, value3, ...)

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to define Tuples
  
# Creating an empty tuple
tupl1 = ()
println(isempty(tupl1))
  
# Creating a tuple with similar values
tupl2 = (1, 2, 4, 5)
println(tupl2)
  
# Creating a tuple with mixed values
tupl3 = (1, 2, 3, "Hello Geeks")
println(tupl3)

chevron_right


Output:
Tuples-Julia-Output-01



Accessing elements from a tuple

Elements of a tuple can be easily extracted with the use of square brackets([]). Loops can also be used to extract more tha one element from a tuple. Also, a range of elements can also be extracted from a tuple by passing a range within square brackets just like strings.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to access elements
# from a Tuple
  
# Creating a tuple
tupl = ("Hello", "Geeks", 1, 2, 3, "Welcome")
  
# Accessing tuple elements
println(tupl[2])
  
# Accessing a range of elements
println(tupl[1:3])
  
# Using for loop to iterate over tuple
for i in tupl
    println(i)
end

chevron_right


Output:
Tuples-Julia-Output-02

Operations on Tuples

Tuples allow performing various operations on them as per user needs. These operations can be like reverse, length, map, isempty, etc.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Julia program to access elements
# from a Tuple
  
# Creating a tuple
tupl = ("Hello", "Geeks", 1, 2, 3, "Welcome")
  
# Reversing tuple
Rev_tupl = reverse(tupl)
println(Rev_tupl)
  
# Printing Tuple length
println(length(tupl))
  
# Using map operator
println(map(typeof, tupl))
  
# Checking if tuple is empty
println(isempty(tupl))

chevron_right


Output:
Tuples-Julia-Output-03




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.