Skip to content

Instantly share code, notes, and snippets.

@Patrikios
Last active September 19, 2022 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Patrikios/e85736caef18f5ebd52fc43c5d382b71 to your computer and use it in GitHub Desktop.
Save Patrikios/e85736caef18f5ebd52fc43c5d382b71 to your computer and use it in GitHub Desktop.
julia pairs data structure
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