如何在不牺牲性能的情况下将函数作为参数传递给 Julia 中的其他函数?
How to pass functions as arguments to other functions in Julia without sacrificing performance?
编辑 尝试解决@user2864740 的编辑和评论:我想知道是否有任何与 0.4rc1/rc2 特别相关的信息,或者特别是某个人的策略或建议的 Julia 开发人员比下面引用的开发人员更新(特别是@StefanKarpinski 在下面#6 中的 2014 年 1 月的回答)。谢谢
请参阅示例
- https://groups.google.com/forum/#!topic/julia-users/pCuDx6jNJzU
- https://groups.google.com/forum/#!topic/julia-users/2kLNdQTGZcA
- https://groups.google.com/forum/#!msg/julia-dev/JEiH96ofclY/_amm9Cah6YAJ
- https://github.com/JuliaLang/julia/pull/10269
- https://github.com/JuliaLang/julia/issues/1090
- Can I add type information to arguments that are functions in Julia?
- Performance penalty using anonymous function in Julia
(作为一个相当缺乏经验的 Julia 用户)我对这些信息的最佳综合(其中一些似乎已经过时)是最佳实践是 "avoid doing this" 或 "use FastAnonymous.jl."
我想知道处理这个问题的最新和最好的方法是什么。
[更长的版本:]
特别是,假设我有一个很大的函数层次结构。我希望能够做类似
的事情
function transform(function_one::Function{from A to B},
function_two::Function{from B to C},
function_three::Function{from A to D})
function::Function{from Set{A} to Dict{C,D}}(set_of_As::Set{A})
Dict{C,D}([function_two(function_one(a)) => function_three(a)
for a in set_of_As])
end
end
请不要从字面上理解代码。这是一个更一般的转换形式的狭义示例,我希望能够执行,而不管转换的实际细节如何,但我希望以一种我不必担心的方式进行(太多)关于检查性能(也就是说,超出我在任何非函数与函数作为参数的情况下应用的正常担忧)每次我编写一个以这种方式运行的函数时。
例如,在我的理想世界中,正确答案应该是 "so long as you annotate each input function with @anon
before you call this function with those functions as arguments, then you're going to do as well as you can without tuning to the specific case of the concrete arguments you're passing."
如果这是真的,那就太好了——我只是想知道这是否是正确的解释,或者如果不是,是否有一些资源我可以阅读这个主题更接近 "logically" 提出的综合比这里的链接集合(更多的是关于这个问题的集体意识或思想史)。
答案仍然是 "use FastAnonymous.jl," 或手动创建 "functor types"(参见 NumericFuns.jl)。
如果您使用的是 julia 0.4,FastAnonymous.jl 的工作方式与官方 "fast closures" 最终在 base julia 中的工作方式基本相同。参见 https://github.com/JuliaLang/julia/issues/11452#issuecomment-125854499。
(FastAnonymous 在 julia 0.3 上以一种非常不同的方式实现,并且有更多的弱点。)
编辑 尝试解决@user2864740 的编辑和评论:我想知道是否有任何与 0.4rc1/rc2 特别相关的信息,或者特别是某个人的策略或建议的 Julia 开发人员比下面引用的开发人员更新(特别是@StefanKarpinski 在下面#6 中的 2014 年 1 月的回答)。谢谢
请参阅示例
- https://groups.google.com/forum/#!topic/julia-users/pCuDx6jNJzU
- https://groups.google.com/forum/#!topic/julia-users/2kLNdQTGZcA
- https://groups.google.com/forum/#!msg/julia-dev/JEiH96ofclY/_amm9Cah6YAJ
- https://github.com/JuliaLang/julia/pull/10269
- https://github.com/JuliaLang/julia/issues/1090
- Can I add type information to arguments that are functions in Julia?
- Performance penalty using anonymous function in Julia
(作为一个相当缺乏经验的 Julia 用户)我对这些信息的最佳综合(其中一些似乎已经过时)是最佳实践是 "avoid doing this" 或 "use FastAnonymous.jl."
我想知道处理这个问题的最新和最好的方法是什么。
[更长的版本:]
特别是,假设我有一个很大的函数层次结构。我希望能够做类似
的事情function transform(function_one::Function{from A to B},
function_two::Function{from B to C},
function_three::Function{from A to D})
function::Function{from Set{A} to Dict{C,D}}(set_of_As::Set{A})
Dict{C,D}([function_two(function_one(a)) => function_three(a)
for a in set_of_As])
end
end
请不要从字面上理解代码。这是一个更一般的转换形式的狭义示例,我希望能够执行,而不管转换的实际细节如何,但我希望以一种我不必担心的方式进行(太多)关于检查性能(也就是说,超出我在任何非函数与函数作为参数的情况下应用的正常担忧)每次我编写一个以这种方式运行的函数时。
例如,在我的理想世界中,正确答案应该是 "so long as you annotate each input function with @anon
before you call this function with those functions as arguments, then you're going to do as well as you can without tuning to the specific case of the concrete arguments you're passing."
如果这是真的,那就太好了——我只是想知道这是否是正确的解释,或者如果不是,是否有一些资源我可以阅读这个主题更接近 "logically" 提出的综合比这里的链接集合(更多的是关于这个问题的集体意识或思想史)。
答案仍然是 "use FastAnonymous.jl," 或手动创建 "functor types"(参见 NumericFuns.jl)。
如果您使用的是 julia 0.4,FastAnonymous.jl 的工作方式与官方 "fast closures" 最终在 base julia 中的工作方式基本相同。参见 https://github.com/JuliaLang/julia/issues/11452#issuecomment-125854499。 (FastAnonymous 在 julia 0.3 上以一种非常不同的方式实现,并且有更多的弱点。)