Categories: Julia

Tuples in Julia

Due to functions being chosen via multiple-dispatch, tuples are inextricably linked to functions. Tuples decide which function is to be chosen for execution as well as what is later returned. Hence, tuples are in fact rendered immutable. That is, once defined, you cannot modify a tuple:

julia> x=(1,2,3)
(1, 2, 3)

julia> x[1]
1

julia> x[1]=10
ERROR: MethodError: no method matching setindex!(::Tuple{Int64,Int64,Int64}, ::Int64, ::Int64)
Stacktrace:
 [1] top-level scope at REPL[70]:1

Again, remembering the link to functions, we have named tuples

julia> x = (a=1, b="two")
(a = 1, b = "two")

julia> x[1],x[2]
(1, "two")

julia> x.a #We can use Javascript-like syntax to access tuple's elements
1

Another familiar way of accessing the values of a tuple is destructuring:

julia> first,second=x
(a = 1, b = "two")

julia> first
1

julia> second
"two"

That’s a wrap! In the next article we’ll talk about dictionaries and sets in Julia.

References

  1. Julia Docs
Editorial Staff

Share
Published by
Editorial Staff

Recent Posts

MapReduce Algorithm

In this tutorial, we will focus on MapReduce Algorithm, its working, example, Word Count Problem,…

1 month ago

Linear Programming using Pyomo

Learn how to use Pyomo Packare to solve linear programming problems. In recent years, with…

8 months ago

Networking and Professional Development for Machine Learning Careers in the USA

In today's rapidly evolving technological landscape, machine learning has emerged as a transformative discipline, revolutionizing…

10 months ago

Predicting Employee Churn in Python

Analyze employee churn, Why employees are leaving the company, and How to predict, who will…

1 year ago

Airflow Operators

Airflow operators are core components of any workflow defined in airflow. The operator represents a…

1 year ago

MLOps Tutorial

Machine Learning Operations (MLOps) is a multi-disciplinary field that combines machine learning and software development…

1 year ago