typestar

Tuples and named tuples in Julia

Fixed-size bundles, with or without labels.

# Tuples are fixed and immutable; named tuples label the slots.
fix = (52.74, 0.39)
lat, lon = fix
println("Site at $lat N, $lon E")

conditions = (seeing = 2.1, transparency = 4, moon = "new")
println("Seeing $(conditions.seeing) arcsec")

# Destructure by property name with the (; ...) form.
(; transparency, moon) = conditions
println("Transparency $transparency/5, $moon moon")

swap = (b, a) -> (a, b)
println(swap(1, 2))

How it works

  1. lat, lon = fix destructures positionally.
  2. (seeing = 2.1, ...) labels fields read with a dot.
  3. (; transparency, moon) = conditions destructures by name.

The run, in numbers

Lines
14
Characters to type
427
Tokens
94
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 87 seconds.

Type this snippet

Step 3 of 3 in Collections, step 12 of 27 in Language basics.

← Previous Next →