Dicts and Sets in Julia
Pairs with =>, membership with in, dedup for free.
# Dicts map keys to values with =>; Sets deduplicate.
exposures = Dict("M42" => 120, "M31" => 300)
exposures["M45"] = 90
println(exposures["M31"])
println(get(exposures, "M13", 0))
for (target, secs) in sort(collect(exposures))
println("$target: $secs s")
end
seen = Set(["Vega", "Altair", "Vega"])
push!(seen, "Deneb")
println("Summer triangle logged? ", length(seen) == 3)
How it works
Dict("M42" => 120)builds; indexing assigns new keys.get(dict, key, default)reads without risking a KeyError.Setdeduplicates on construction and onpush!.
Keywords and builtins used here
DictSetendforin
The run, in numbers
- Lines
- 14
- Characters to type
- 377
- Tokens
- 110
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 102 seconds.
Step 2 of 3 in Collections, step 11 of 27 in Language basics.