# Possibly heterogeneous (values can be of different types)
typeof((2, 'a', 1.0, "test"))Tuple{Int64, Char, Float64, String}
Marie-Hélène Burle
Values can be stored in collections. This section introduces tuples, dictionaries, sets, and arrays in Julia.
Tuples are immutable, indexable, and possibly heterogeneous collections of elements. The order of elements matters.
Tuple{Int64, Char, Float64, String}
MethodError: no method matching setindex!(::Tuple{Int64, Char, Float64, String}, ::Int64, ::Int64)
The function `setindex!` exists, but no method is defined for this combination of argument types.
Stacktrace:
[1] top-level scope
@ ~/parvus/prog/mint/julia/intro_collections.qmd:32
Tuples can have named components:
Julia also has dictionaries: associative collections of key/value pairs:
Dict{String, Any} with 3 entries:
"Index" => 0.3
"Age" => 52
"Name" => "Roger"
"Name", "Age", and "Index" are the keys; "Roger", 52, and 0.3 are the values.
The => operator is the same as the Pair function:
Dictionaries can be heterogeneous (as in this example) and the order doesn’t matter. They are also indexable:
And mutable (they can be modified):
Sets are collections without duplicates. The order of elements doesn’t matter.
Notice how this is a set of 5 (and not 6) elements: the duplicated 8 didn’t matter.
You can compare sets:
Set{Int64} with 7 elements:
4
7
2
10
9
8
3
Set{Int64} with 1 element:
2
Set{Int64} with 4 elements:
4
7
9
8
Sets can be heterogeneous:
Unidimensional arrays in Julia are called vectors.
These 3 syntaxes are equivalent:
These 4 syntaxes are equivalent:
Elements separated by semi-colons or end of lines get expanded vertically.
Those separated by commas do not get expanded.
Elements separated by spaces or tabs get expanded horizontally.
Your turn:
Compare the outputs of the following:
In Julia, arrays can be heterogeneous:
This is possible because all elements of an array, no matter of what types, will always sit below the Any type in the type hierarchy.
Below are examples of some of the functions initializing arrays:
2×3×4 Array{Float64, 3}:
[:, :, 1] =
0.895753 0.283787 0.0329254
0.282173 0.620822 0.946778
[:, :, 2] =
0.377984 0.894322 0.867468
0.00559226 0.0429895 0.199454
[:, :, 3] =
0.931746 0.822187 0.0219664
0.720164 0.457989 0.284958
[:, :, 4] =
0.784784 0.731742 0.42645
0.882276 0.448076 0.97614
2×3×4 Array{Int64, 3}:
[:, :, 1] =
-7125592557592274203 -5164888487477981947 2857801171650174190
-9000043744487872212 -5344160693932584506 -8206812920913940183
[:, :, 2] =
7532566866337392625 4769412482333805350 -2141250961581510129
-5233455439209188314 7731871886451166936 -1314134804185320675
[:, :, 3] =
6528776247353209651 -7503588944027516077 8806414176287262627
-4989881567933742921 -324395129996211358 -4330561309157048261
[:, :, 4] =
-959032162924631606 -7029310787635631030 -3190838497061698567
-7418260087189384225 8080331417591570394 1952259309296659801
To apply a function to each element of a collection rather than to the collection as a whole, Julia uses broadcasting.
LoadError: MethodError: no method matching abs(::Vector{Int64})
This doesn’t work because the function abs only applies to single elements.
By broadcasting abs, you apply it to each element of a:
The dot notation is equivalent:
It can also be applied to the pipe, to unary and binary operators, etc.
Your turn:
Try to understand the difference between the following 2 expressions:
Hint: 0/1 are a short-form notations for false/true in arrays of Booleans.
Julia has an array comprehension syntax similar to Python’s:
As in other mathematically oriented languages such as R, Julia starts indexing at 1.
Indexing is done with square brackets:
Your turn:
Index the element on the 3rd row and 2nd column of b:
Your turn:
How can I get the second column?
How can I get the tuple (2, 4)? (a tuple is a list of elements)
As in Python, by default, arrays are passed by sharing:
This prevents the unwanted copying of arrays.