Julia Tuples

Julia's tuples are similar to arrays; both are ordered collections of elements, the difference being that tuple elements cannot be modified.

In addition, tuples use parentheses(...)while arrays use square brackets[...]。

Creating a tuple is very simple: just add elements in parentheses and separate them with commas. Many functions for arrays can also be used with tuples.

As in the example below:

Example

julia> tupl=(5,10,15,20,25,30)   # Create a tuple
(5, 10, 15, 20, 25, 30)

julia> tupl
(5, 10, 15, 20, 25, 30)

julia> tupl[3:end]          # Output the tuple from the third to the last element
(15, 20, 25, 30)

julia> tupl = ((1,2),(3,4))   # Create a two-dimensional tuple
((1, 2), (3, 4))

julia> tupl[1]      # Access a 2D tuple element, outputting the first-dimension tuple
(1, 2)

julia> tupl[1][2]    # Access a 2D tuple element, outputting the second element of the first-dimension tuple
2

The elements of a tuple cannot be modified. If we try to modify it, an error will be reported:

Example

julia> tupl2=(1,2,3,4)
(1, 2, 3, 4)

julia> tupl2[2]=0
ERROR: MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64)
Stacktrace:
 [1] top-level scope
   @ REPL[8]:1

Tuple Naming

We can name tuples, making it more convenient to access them.

The following lists several different ways to name tuples.

1. Naming the key and value separately in a tuple

The key and value in a tuple can be named separately and independently, as in the example below:

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 use.a dot to access the tuple:

Example

julia> shape_item2.corner1
(100, 100)

julia> shape_item2.corner2
(200, 200)

2. Key and value together in one tuple

Key and value can both be in one tuple, as in the example below:

Example

julia> shape_item = (corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

We can use.a dot to access the tuple:

Example

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 values just like using a regular tuple, as shown below:

Example

julia> c1, c2, center = shape_item
(corner1 = (1, 1), corner2 = (-1, -1), center = (0, 0))

julia> c1
(1, 1)

3. Merging two named tuples

We can usemerge()a function to merge two named tuples, as in the example below:

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")

Tuples as Function Arguments

In the following example, we create atestFuncfunction, and pass the tupleoptionsas an argument:

Example: test.jl file code

# Create a function
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
 end

# Create a tuple
 options = (b = 200, c = 300)
 
# Execute the function, tuple passed as an argument
 testFunc(1, 2, 3; options...)

Use thejuliacommand to execute the above file, the output is:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300

If the specified arguments are placed after the tuple, they will override the existing arguments in the tuple:

Example

# Create a function
function testFunc(x, y, z; a=10, b=20, c=30)
    println("x = $x, y = $y, z = $z; a = $a, b = $b, c = $c")
 end

# Create a tuple
 options = (b = 200, c = 300)

# Execute the function, tuple passed as an argument; specified arguments before the tuple will not override
testFunc(1, 2, 3; b = 1000_000, options...)

# Execute the function, tuple passed as an argument; specified arguments after the tuple will override
testFunc(1, 2, 3; options..., b= 1000_000)

Use thejuliacommand to execute the above file, the output is:

$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300
x = 1, y = 2, z = 3; a = 10, b = 1000000, c = 300
Other Extensions