I want to write a function which includes both functions and accepts all parameters of functions. What is the best way to do this? Do I have to write all the arguments of f1 and f2 in the function?
Thank you
It doesn’t look elegant and doesn’t scale if there are many functions.
Is there a way to take parameters for functions f1, f2 without explicitly writing them in f?
f1 and f2 are in a Pkg. I didn’t write them.
I have to call them in a function but I want this function to accept the parameters for f1 and f2 as well.
# examples
f1(a1; a2=1, a3="a") = (a1, a2, a3)
f2(b1; b2='k', b3=2.0) = (b1, b2, b3)
function namedArgumentsOf(func, args...)
func_args = @which func(args...)
Base.kwarg_decl(func_args)
end
function f(a1, b1; params...)
args_f1 = [i for i in params if i[1] in namedArgumentsOf(f1, 1)]
args_f2 = [i for i in params if i[1] in namedArgumentsOf(f2, 1)]
f1(a1; args_f1...) |> println
f2(b1; args_f2...) |> println
end
f(3, 5; a2=4, a3=7, b2='e')
A tiny detail for multiple dispatch to work, probably is should be namedArgumentsOf(f1, a1) (and similar for f2) to ensure that it uses the right method depending on the type of a1.
function f(a1, b1; args_f1=(a2=3, a3="b"), args_f2=()) # If I declare args different from the default ones of f1 (or f2)
f1(a1; args_f1...) |> println
f2(b1; args_f2...) |> println
end
f(1, 2; args_f1=(a2=4,)) # I don't get a3 = "b"
f(1, 2; args_f1=(a2=4,a3="b")) # I have to write a3="b" again