首页 > 解决方案 > 有没有办法在 Julia 中对函数进行深度复制?

问题描述

和标题说的差不多。如果我 assigng=f或 do g=deepcopy(f),结果是一样的:重新定义f也会重新定义g。有没有办法让 g 成为 f 的独立副本?

julia> function f(x) x end
f (generic function with 1 method)

julia> f(1)
1

julia> g = deepcopy(f)
f (generic function with 1 method)

julia> g(1)
1

julia> function f(x) x+1 end
f (generic function with 1 method)

julia> f(1)
2

julia> g(1)
2

标签: functioncopyjulia

解决方案


Julia 中的 AFAICT 函数被认为是位类型:

julia> f(x) = x
f (generic function with 1 method)

julia> isbitstype(typeof(f))
true

这意味着deepcopy对他们来说是无操作的。这是 的定义deepcopy

function deepcopy(x)
    isbitstype(typeof(x)) && return x
    return deepcopy_internal(x, IdDict())::typeof(x)
end

这样做的原因是每个函数都有自己的类型(但它可以有很多方法)。

phipsgabler 提出的解决方案是可行的,因为每次定义匿名函数时,它都会获得自己的新类型。看这里:

julia> typeof(x -> x)
var"#1#2"

julia> typeof(x -> x)
var"#3#4"

julia> typeof(x -> x)
var"#5#6"

不过,这有一个缺点——每次你将一个新的匿名函数传递给另一个函数时,它都必须被编译,例如:

julia> @time map(x -> x, 1:2);
  0.024220 seconds (49.61 k allocations: 2.734 MiB)

julia> @time map(x -> x, 1:2);
  0.023754 seconds (48.25 k allocations: 2.530 MiB)

julia> @time map(x -> x, 1:2);
  0.023336 seconds (48.25 k allocations: 2.530 MiB)

对比

julia> fun = x -> x
#7 (generic function with 1 method)

julia> @time map(fun, 1:2);
  0.023459 seconds (48.23 k allocations: 2.530 MiB)

julia> @time map(fun, 1:2);
  0.000016 seconds (4 allocations: 192 bytes)

julia> @time map(fun, 1:2);
  0.000013 seconds (4 allocations: 192 bytes)

回到你原来的问题。即使你写了这样的东西:

julia> f(x) = x
f (generic function with 1 method)

julia> g = x -> f(x)
#1 (generic function with 1 method)

julia> g(1)
1

你得到:

julia> f(x) = 2x
f (generic function with 1 method)

julia> g(1)
2

因为f(x) = 2x替换了函数的方法,f但它的类型没有改变(如上所述 - 一个函数可以有很多方法,您甚至可以更新函数的方法,如上例所示)。这与您在 Julia 手册中解释所谓“世界时代”问题有关。


推荐阅读