gpt4 book ai didi

julia - 确定为给定的函数调用调度哪个方法

转载 作者:行者123 更新时间:2023-12-04 18:00:22 25 4
gpt4 key购买 nike

考虑 findfirst功能。我可以看到为 findfirst 定义了哪些方法通过使用 methods :

julia> methods(findfirst)
# 9 methods for generic function "findfirst":
[1] findfirst(A::Union{AbstractString, AbstractArray}) in Base at array.jl:1672
[2] findfirst(p::Union{Base.Fix2{typeof(==),T}, Base.Fix2{typeof(isequal),T}}, r::StepRange{T,S}) where {T, S} in Base at array.jl:1758
[3] findfirst(pred::Base.Fix2{#s66,#s65} where #s65<:Union{Int8, UInt8} where #s66<:Union{typeof(==), typeof(isequal)}, a::Union{Array{Int8,1}, Array{UInt8,1}}) in Base at strings/search.jl:22
[4] findfirst(testf::Function, A::Union{AbstractString, AbstractArray}) in Base at array.jl:1754
[5] findfirst(testf::Function, A) in Base at array.jl:1747
[6] findfirst(pattern::AbstractString, string::AbstractString) in Base at strings/search.jl:104
[7] findfirst(ch::AbstractChar, string::AbstractString) in Base at strings/search.jl:124
[8] findfirst(r::Regex, s::AbstractString) in Base at regex.jl:327
[9] findfirst(A) in Base at array.jl:1663

现在假设我想弄清楚当我调用 findfirst(iseven, 1:4) 时,哪些方法被调度了。 .我怎么做?

最佳答案

您可以使用 @which 宏:

julia> @which findfirst(iseven, 1:4)
findfirst(testf::Function, A::Union{AbstractString, AbstractArray}) in Base at array.jl:1754

另一个例子:
julia> @which length(1:7)
length(r::AbstractUnitRange{T}) where T<:Union{Int128, Int64} in Base at range.jl:542

julia> @which length("hello world")
length(s::String) in Base at strings/string.jl:259

编辑:

正如@giordano 在评论中指出的那样,您可以使用
@less findfirst(iseven, 1:4)

或者
@edit findfirst(iseven, 1:4)

如果您想查看将被调度的方法的源代码。 @less使用系统寻呼机显示源代码。 @edit将在文本编辑器中打开包含方法源代码的文件。您可以通过设置 JULIA_EDITOR 来控制打开哪个编辑器。 environment variable .例如,您可以将编辑器设置为 vim 通过调用
ENV["JULIA_EDITOR"] = "vim"

在 REPL 或您的启动文件中, ~/.julia/config/startup.jl .或者,您可以添加 export JULIA_EDITOR=vim给您的 .bashrc.bash_profile linux/macos 上的文件。

关于julia - 确定为给定的函数调用调度哪个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60030694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com