Dictionary - inverse and consolidating

Edit: Thank you @kristoffer.carlsson for pointing out my oversight and the code is working now.

# I forgot the `( )`. Fixed now
dict = Dict{Any, Any}()

# The doc example
get!(dict, :a) do
    time()
end

# The equivalent
get!( ()-> time(), dict, :b)

# If you already have function
function f()
    return time()
end

get!(f, dict, :c)

println(duct)  # Dict{Any,Any}(:a => 1.593679792519603e9,:b => 1.59367979306765e9,:c => 1.59367992264622e9)

My original post:

AFAIK,

  1. do block in Julia replaces the first parameter (f::Function) of get!(). Use do block if the function is more than a one-liner, for readability.
  2. time() is to give a value to each element in dict. It can be anything as you see fit.

So it is equivalent to

get!(x-> time(), dict, key)
1 Like