function - 有没有办法在 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
解决方案
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 手册中解释的所谓“世界时代”问题有关。
推荐阅读
- javascript - 为什么我无法加载 ejs 文件?
- amazon-web-services - Kubernetes:外部机密操作错误:InvalidClientTokenId:请求中包含的安全令牌无效
- r - 获取数据框中多个预测变量和结果的 AUC
- python - GitPython 列出分支,如果它们匹配某个名称模式,则删除
- node.js - 是否可以为 NodeJS 工作线程构造函数使用字符串?
- javascript - 无法使用 react-router-dom 5.2.0 编写嵌套路由?
- typescript - 由于以下原因无法运行 mocha:TS2451:无法重新声明块范围变量 x
- spring - 侦听器在使用 EmbeddedKafka Spring 进行测试时不使用消息
- r - R脚本生成两个相同列表的所有组合,包括不完整列表
- java - Apache James 中的嵌入式 Tomcat