I am currently migrating from R. In R, i used the “do.call” function for many of own functions, which is a generic function that allows to call any arbitrary R function with its arguments in a string. This comes handy if you work with a lot of different distributions in a bigger script - that way I can change sampling procedure relatively easy and fast. Is there something similar in Julia?
I thought I could instead just create a module where I use the match package (Switch-Case seems to be not supported in Julia?) and manually add the distributions of interest, but the above case seems more convenient to me and I can also use it for other cases. I would assume that the latter case is faster though?
I’d suggest checking out the Distributions.jl package for this kind of thing. Then you can just pass a distribution object to your function and rand should just work. So something like
using Distributions
function f(D, n)
x = rand(D, n)
mean(x)
end
will work no matter if you pass Normal(2, 3), InverseGamma(4, 7), or even a new distribution that you create and define a rand method for. @BeastyBlacksmith’s solution above will certainly work, but you’ll get a lot more flexibility using a package like Distributions.jl than dealing with just the rand* functions in Base.
Note the following difference between arg splatting params... and keyword splatting ;params....
IMO arg splatting with named tuples is a source of bugs and should be avoided.
julia> params = (n = 1000, mu = 1, sd = 2)
(n = 1000, mu = 1, sd = 2)
julia> f(args...;kw...) = args, kw
f (generic function with 1 method)
julia> f(params...)
((1000, 1, 2), Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}())
julia> f(;params...)
((), Base.Iterators.Pairs(:n => 1000,:mu => 1,:sd => 2))