Last active
September 19, 2022 13:12
-
-
Save Patrikios/e85736caef18f5ebd52fc43c5d382b71 to your computer and use it in GitHub Desktop.
julia pairs data structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a_tuple = (1, 2) | |
a_pair = 1 => 2 | |
a_tuple != a_pair | |
# true | |
# The elements are stored in the fields first and second | |
a_pair.first | |
# 1 | |
a_pair.second | |
# 1 | |
typeof(a_pair) | |
# Pair{Int64, Int64} | |
# construct pair with 'Pair' keyword | |
Pair(1, 32.1) | |
# They can also be accessed via iteration | |
p = "foo" => 7 | |
for x in p | |
println(x) | |
end | |
# foo | |
# 7 | |
# but a Pair is treated as a single "scalar" for broadcasting operations | |
r = "Pat" => "Mat" | |
r2 = [r.first, r.second] | |
uppercase(r2) | |
# ERROR | |
uppercase.(r2) | |
# 2-element Vector{String}: | |
# "PAT" | |
# "MAT" | |
uppercase(r) | |
# ERROR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment