Tuples
Tuple is a data type like Array, the only thing is you can’t change values in a Tuple, in Array you can. You can get the Jupyter notebook for this blog here https://gitlab.com/data-science-with-julia/code/-/blob/master/tuples.ipynb.
So let’s dive in.
First we create a Tuple and assign it to a variable called tuple
tuple = (1, 5, 9, 3, 7, 2)
Output:
(1, 5, 9, 3, 7, 2)
So the above example speaks something, in Arrays we wrap elements with square brackets []
, but here we are wrapping it with rounded ones ()
.
In the below example we check the Tuple’s type:
typeof(tuple)
Output:
NTuple{6,Int64}
I am not sure why its called NTuple
, I read some docs and I infer it means a homogeneous data type. In the above example our tuple
contains all Int64
that is integers of 64 bit wide; and it has 6
elements in them, identified by 6
in NTuple{6,Int64}
.
An array is mutable, that is the values in it can be changed, below we have an Array:
array = [1, 5, 9, 3, 7, 2]
Output:
6-element Array{Int64,1}:
1
5
9
3
7
2
and we change the third element in it:
array[3] = 10
array
Output:
6-element Array{Int64,1}:
1
5
10
3
7
2
This statement array[3] = 10
accesses the third element and sets it to 10
. If we try to do this in Tuple as shown below, we get an error:
tuple[3] = 10
tuple
Output:
MethodError: no method matching setindex!(::NTuple{6,Int64}, ::Int64, ::Int64)
Stacktrace:
[1] top-level scope at In[5]:1
[2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[3] execute_code(::String, ::String) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[5] #invokelatest#1 at ./essentials.jl:710 [inlined]
[6] invokelatest at ./essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[8] (::IJulia.var"#15#18")() at ./task.jl:356
Like an Array we can get maximum value in a Tuple using the maximum()
function:
maximum(tuple)
Output:
9
We can get minimum value in a Tuple using the minimum()
function:
minimum(tuple)
Output:
1
We can get total of Tuple using the sum()
function:
sum(tuple)
Output:
27
Below we get the cumulative sum using cumsum()
function:
cumsum(tuple)
Output:
(1, 6, 15, 18, 25, 27)
We are unable to sort it however, I did not try out sort!()
because anything with a bang modifies the argument that’s been passed to it. A Tuple is immutable and hence sort!()
makes no sense and hence I tried sort()
and it did not work:
sort(tuple)
Output:
MethodError: no method matching sort(::NTuple{6,Int64})
Closest candidates are:
sort(!Matched::AbstractUnitRange) at range.jl:1014
sort(!Matched::AbstractRange) at range.jl:1017
sort(!Matched::SparseArrays.SparseVector{Tv,Ti}; kws...) where {Tv, Ti} at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/SparseArrays/src/sparsevector.jl:1912
...
Stacktrace:
[1] top-level scope at In[14]:1
[2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091
[3] execute_code(::String, ::String) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:27
[4] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/execute_request.jl:86
[5] #invokelatest#1 at ./essentials.jl:710 [inlined]
[6] invokelatest at ./essentials.jl:709 [inlined]
[7] eventloop(::ZMQ.Socket) at /home/karthikeyan/.julia/packages/IJulia/a1SNk/src/eventloop.jl:8
[8] (::IJulia.var"#15#18")() at ./task.jl:356
Possibly one must convert Tuple into Array to sort.
Now let’s use our LinearAlgebra
package to and wee if ⋅
operator works on Tuple:
using LinearAlgebra
tuple ⋅ tuple
Output:
169
and it does. To get the dot operator type \cdot
followed by Tab
.
In our last blog about Array, we saw that when getting size of an array we got something as shown:
size_of_array = size(array)
Output:
(6,)
Well the size of an array is a Tuple. You also see that in the above example it gives an Output (6,)
and not (6)
. Now let’s inspect the type of the returned value:
typeof(size_of_array)
Output:
Tuple{Int64}
As an exercise, why don’t you try typeof((6))
and see what it is? A Tuple should always have an comma ,
, thats how Julia identifies it’s an Tuple than a number surrounded by brackets.
I wish we could convert Tuple to Array by using a construct like this Tuple(<array>)
, but it turns out that you must do a trickery as shown below:
array_from_tuple = [element for element in tuple]
Output:
6-element Array{Int64,1}:
1
5
9
3
7
2
In the above example notice this [element for element in tuple]
, this is called list comprehension, it works like this, it starts with an empty Array []
, now take this for element in tuple
, this is a for
loop, so for each element in tuple
, it take an element one by one and puts it into variable named element
, now this element is added to Array here [element ....]
, so in the first iteration it becomes [1]
, in the next [1, 5]
and so on… Finally we get an Array [1, 5, 9, 3, 7, 2]
which gets assigned to array_from_tuple
.
Though making an Array from Tuple is a pain, making a Tuple from an Array seems to be as easy as shown:
tuple_from_array = Tuple(array)
Output:
(1, 5, 10, 3, 7, 2)
weird Julia.