Similar to an array, tuple is also an ordered set of elements. Tuples work in almost the same way as arrays but there are following important differences between them −
-
An array is represented by square brackets whereas a tuple is represented by parentheses and commas.
-
Tuples are immutable.
Creating tuples
We can create tuples as arrays and most of the array’s functions can be used on tuples also. Some of the example are given below −
julia> tupl=(5,10,15,20,25,30)(5, 10, 15, 20, 25, 30)julia> tupl(5, 10, 15, 20, 25, 30)julia> tupl[3:end](15, 20, 25, 30)julia> tupl = ((1,2),(3,4))((1, 2), (3, 4))julia> tupl[1](1, 2)julia> tupl[1][2]2We cannot change a tuple:julia> tupl[2]=0ERROR: MethodError: no method matching setindex!(::Tuple{Tuple{Int64,Int64},Tuple{Int64,Int64}}, ::Int64, ::Int64)Stacktrace: [1] top-level scope at REPL[7]:1
Named tuples
A named tuple is simply a combination of a tuple and a dictionary because −
-
A named tuple is ordered and immutable like a tuple and
-
Like a dictionary in named tuple, each element has a unique key which can be used to access it.
In next section, let us see how we can create named tuples −
Creating named tuples
You can create named tuples in Julia by −
-
Providing keys and values in separate tuples
-
Providing keys and values in a single tuple
-
Combining two existing named tuples
Keys and values in separate tuples
One way to create named tuples is by providing keys and values in separate tuples.
Example
julia> names_shape = (:corner1, :corner2)(:corner1, :corner2)julia> values_shape = ((100, 100), (200, 200))((100, 100), (200, 200))julia> shape_item2 = NamedTuple{names_shape}(values_shape)(corner1 = (100, 100), corner2 = (200, 200))
We can access the elements by using dot(.) syntax −
julia> shape_item2.corner1(100, 100)julia> shape_item2.corner2(200, 200)
Keys and values in a single tuple
We can also create named tuples by providing keys and values in a single tuple.
Example
julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
We can access the elements by using dot(.) syntax −
julia> shape_item.corner1(1, 1)julia> shape_item.corner2(-1, -1)julia> shape_item.center(0, 0)julia> (shape_item.center,shape_item.corner2)((0, 0), (-1, -1))
We can also access all the values as with ordinary tuples as follows −
julia> c1, c2, center = shape_item(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))julia> c1(1, 1)
Combining two named tuples
Julia provides us a way to make new named tuples by combining two named tuples together as follows −
Example
julia> colors_shape = (top = "red", bottom = "green")(top = "red", bottom = "green")julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))julia> merge(shape_item, colors_shape)(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0), top = "red", bottom = "green")
Named tuples as keyword arguments
If you want to pass a group of keyword arguments to a function, named tuple is a convenient way to do so in Julia. Following is the example of a function that accepts three keyword arguments −
julia> function ABC(x, y, z; a=10, b=20, c=30) println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c") endABC (generic function with 1 method)
It is also possible to define a named tuple which contains the names as well values for one or more keywords as follows −
julia> options = (b = 200, c = 300)(b = 200, c = 300)
In order to pass the named tuples to the function we need to use; while calling the function −
julia> ABC(1, 2, 3; options...)x = 1, y = 2, z = 3; a = 10, b = 200, c = 300
The values and keyword can also be overridden by later function as follows −
julia> ABC(1, 2, 3; b = 1000_000, options...)x = 1, y = 2, z = 3; a = 10, b = 200, c = 300julia> ABC(1, 2, 3; options..., b= 1000_000)x = 1, y = 2, z = 3; a = 10, b = 1000000, c = 300