Julia 函数名称大写

Uppercase in name of a function in Julia

我对 Julia 还很陌生,对以下代码感到困惑。在定义了一个函数 LucasTree 之后,它再次被用作 lt。 Julia 是否有某种角色可以让我使用大写缩写来回忆函数?如果是这样,我在哪里可以找到有关此功能的很好的参考资料?

function LucasTree(;γ = 2.0,
                   β = 0.95,
                   α = 0.9,
                   σ = 0.1,
                   grid_size = 100)

   ϕ = LogNormal(0.0, σ)
   shocks = rand(ϕ, 500)

   # build a grid with mass around stationary distribution
   ssd = σ / sqrt(1 - α^2)
   grid_min, grid_max = exp(-4ssd), exp(4ssd)
   grid = range(grid_min, grid_max, length = grid_size)

   # set h(y) = β * int u'(G(y,z)) G(y,z) ϕ(dz)
   h = similar(grid)
   for (i, y) in enumerate(grid)
       h[i] = β * mean((y^α .* shocks).^(1 - γ))
   end

   return (γ = γ, β = β, α = α, σ = σ, ϕ = ϕ, grid = grid, shocks = shocks, h = h)
end
function lucas_operator(lt, f)

    # unpack input
    @unpack grid, α, β, h = lt
    z = lt.shocks

    Af = LinearInterpolation(grid, f, extrapolation_bc=Line())

    Tf = [ h[i] + β * mean(Af.(grid[i]^α .* z)) for i in 1:length(grid) ]
    return Tf
end

不,这不存在。它太不唯一而不实用,而且 Julia 中的函数名称按照惯例 应该 完全小写(structs/types 可以是 CamelCase,但不是函数,可能的例外是构造函数*).

无论如何,在您发布的代码中,函数 lucas_operator 有两个参数,ltf,然后可以使用它们在 lucas_operator 函数中。这些原则上可以是任何东西,无论它们在函数范围之外如何命名,它们在函数范围内都将被命名为 ltf。例如:

function example(foo, bar)
    return foo(2*bar)
end

如果您随后致电

example(somereallylongfunctionname, somevariable)

那么 return 相当于

somereallylongfunctionname(2*somevariable)

或类似的

example(SomeImproperlyCapitalizedFunction, somevariable)
# equivalent to SomeImproperlyCapitalizedFunction(2*somevariable)

在任何一种情况下,无论其名称在 example 函数范围之外,传递给函数的第一个参数在函数内将被称为 foo

* 除了构造函数:这将是一个用于构造自定义类型的函数。这 相当 这样做,但它 return 一个 NamedTuple 的实例,然后似乎有点像 type/struct后续代码,所以也许它可以算作构造函数。