Julia Tuples
Julia's tuples are similar to arrays in that both are ordered collections of elements; the difference is that tuple elements cannot be modified, and tuples are immutable types.
In addition, tuples use parentheses (...), while arrays use square brackets [...].
Creating a tuple is simple: just add elements inside parentheses separated by commas; many array functions can also be used on tuples.
Example
julia
tupl =
(
5
,
10
,
15
,
20
,
25
,
30
)
Create a tuple
(
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 nested tuple
(
(
1
,
2
)
,
(
3
,
4
)
)
julia
tupl
[
1
]
Access nested tuple elements and output the first tuple
(
1
,
2
)
julia
tupl
[
1
]
[
2
]
Output the second element of the first tuple
2
Note:A single-element tuple must have an extra comma, written as
(1,). Because(1)is just the number 1 in parentheses, not a tuple. The empty tuple is written as().
Example
julia
(
1
,
)
(
1
,
)
julia
(
1
)
This is just the number 1
1
julia
typeof
(
(
1
,
)
)
Tuple
{
Int64
}
Tuples are immutable
Tuple elements cannot be modified; trying to modify one raises an error:
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:
[
...
]
Because they are immutable, tuples can be safely shared in many places, can be used as dictionary keys, and the compiler can perform more optimizations on them.
Destructuring Assignment
One of the most common uses of tuples is destructuring assignment: assigning the values of a tuple (or any iterable) to multiple variables at once:
Example
julia
x, y =
(
1
,
2
)
Destructuring Assignment
(
1
,
2
)
julia
x
1
julia
y
2
Swapping two variables requires no temporary variable
julia
x, y = y, x
(
2
,
1
)
Use _ to ignore elements you don't care about
julia
_, b =
(
10
,
20
)
(
10
,
20
)
julia
b
20
Named Tuples (NamedTuple)
We can name the elements of a tuple so they can be accessed like fields.
1. Name keys and values separately
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 the . dot to access elements:
Example
julia
shape_item2.
corner1
(
100
,
100
)
julia
shape_item2.
corner2
(
200
,
200
)
2. Keys and values together in one tuple
The more common form is to name each element directly in the literal:
Example
julia
shape_item =
(
corner1 =
(
1
,
1
)
, corner2 =
(
1
, -
1
)
, center =
(
0
,
0
)
)
(
corner1 =
(
1
,
1
)
, corner2 =
(
1
, -
1
)
, center =
(
0
,
0
)
)
julia
shape_item.
corner1
(
1
,
1
)
julia
shape_item.
center
(
0
,
0
)
We can also destructure all values just like a regular tuple:
Example
julia
c1, c2, center = shape_item
(
corner1 =
(
1
,
1
)
, corner2 =
(
1
, -
1
)
, center =
(
0
,
0
)
)
julia
c1
(
1
,
1
)
The type of a named tuple can be written with the @NamedTuple macro, similar to the declaration syntax of struct:
Example
julia
typeof
(
(
a=
1
, b=
"hello"
)
)
@NamedTuple
{
a::
Int64
, b::
String
}
Declare a named tuple type with the macro; it can also be used as a constructor
julia
@NamedTuple
{
a::
Float32
, b::
String
}
(
(
1
,
""
)
)
(
a = 1.0f0, b =
""
)
3. Merge two named tuples
We can use the merge() function to merge two named tuples; if keys are duplicated, later values override earlier ones:
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
A named tuple can be passed wholesale as keyword arguments to a function, expanded with tuple_name...:
Example: test.jl file code
Define 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 named tuple
options =
(
b =
200
, c =
300
)
Call the function, passing the tuple as keyword arguments
testFunc
(
1
,
2
,
3
;
options...
)
Run the above file with the julia command; the output is:
$ julia test.jl
x = 1, y = 2, z = 3; a = 10, b = 200, c = 300
Explicit keyword arguments placed before the tuple are overridden by the tuple; placed after the tuple, they override the values in the tuple:
Example
Explicit arguments before the tuple are overridden by b=200 in the tuple
julia
testFunc
(
1
,
2
,
3
;
b =
1000000
, options...
)
x =
1
, y =
2
, z =
3
;
a =
10
, b =
200
, c =
300
Explicit arguments after the tuple override b in the tuple
julia
testFunc
(
1
,
2
,
3
;
options..., b =
1000000
)
x =
1
, y =
2
, z =
3
;
a =
10
, b =
1000000
, c =
300
Tuples vs. Arrays
| Feature | Tuples (Tuple) | Arrays (Vector) |
|---|---|---|
| Syntax | (1, 2, 3) |
[1, 2, 3] |
| Mutability | Immutable | Mutable |
| Element type | Each element's type is recorded independently | All elements have the same type |
| Can be used as a dictionary key | You can | No |
| Typical use | Multiple return values from functions, fixed-structure data | Dynamic collections, numerical computing |