Define a function such that it only executes its body once. The value from first execution is memoized at the module level.
module X
export once
let x = Ref{Any}()
global function once(f::Function)
isassigned(x) && return x[]
x[] = f()
return x[]
end
end
end #module
REPL:
julia> using .X: once
julia> once() do
println("cool")
1
end
cool
1
julia> once() do
println("cool")
1
end
1