Hi,
I’m trying to populate a Dictionary{String, Function} with the exported methods found in StatsBase so that I can expose them to a UI and I’m getting unexpected results:
using StatsBase
d = Dict{String,Function}()
functions = names(StatsBase, all= false, imported = false)
#print(functions)
for f in functions
println(typeof(f))
# func = f
d[string(f)] = getfield(StatsBase, f)
end
returns:
Symbol
MethodError: Cannot `convert` an object of type Type{AbstractDataTransform} to an object of type Function
Closest candidates are:
convert(::Type{T}, !Matched::T) where T at essentials.jl:171
Stacktrace:
[1] setindex!(::Dict{String,Function}, ::Type{T} where T, ::String) at .\dict.jl:380
[2] top-level scope at .\In[61]:11
[3] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091
As you can see, type checking f returns Symbol, further (taking from an earlier example) the following works:
using StatsBase
fn = "percentile"
v = [i for i in 1:10]
p = getfield(StatsBase, Symbol(fn)) :: Function
#test output - percentile(v , [10, 50, 75])
p(v , [10, 50, 75])
is this expected? Note that I also casted the original code to :: Function originally, but a similar error was thrown.
Further, the intended usage of the dictionary is to facilitate an expedient way to obtain an instance of the function and then apply parameters in a standard factory pattern to reduce the “get field” overhead and copy an instance to apply parameters to. e.g. from the above:
d = Dict{String,Function}()
v = [i for i in 1:10]
localpercentile = copy(d["percentile"]) #?
results = localpercentile(v, [10,25,50])
is this possible?
Regards