# Generic function to call arbitrary Julia function and its arguments

**URL:** https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407
**Category:** New to Julia
**Created:** [June 18, 2019, 12:53pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407 "2019-06-18T12:53:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)
#### Post date: [June 18, 2019, 12:53pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/1 "2019-06-18T12:53:15Z")

</div>

Hi there,

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?

Something like:

```julia
func = randn
params = "n = 1000, mu = 1, sd = 2"
do.call(func, params)

```

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?

Thank you for your help!

Best regards,

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [June 18, 2019, 12:59pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/2 "2019-06-18T12:59:10Z")

</div>

First, its good to quote your code with ```.

Second, you can store your parameters as a `NamedTuple` and splat that into you distribution. Like

```julia
func = randn
params = (n=1000, mu = 1, sd = 2)
func(;params...)

```

---

<div class="post-metadata">

### Author: ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)
#### Post date: [June 18, 2019, 1:29pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/3 "2019-06-18T13:29:26Z")

</div>

Thank you, fantastic and exactly what I was looking for!

---

<div class="post-metadata">

### Author: ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)
#### Post date: [June 18, 2019, 2:16pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/4 "2019-06-18T14:16:53Z")

</div>

I’d suggest checking out the [Distributions.jl](https://github.com/JuliaStats/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

```julia
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.

---

<div class="post-metadata">

### Author: ![mrVeng](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrveng/32/8836_2.png) [@mrVeng](https://discourse.julialang.org/u/mrVeng)
#### Post date: [June 18, 2019, 2:56pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/5 "2019-06-18T14:56:50Z")

</div>

> [@jkbest2](#):
>
> using Distributions function f(D, n) x = rand(D, n) mean(x) end

Thank you! Distributions.jl looks really nice I will have a look at it!

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [June 18, 2019, 3:03pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/6 "2019-06-18T15:03:59Z")

</div>

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
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))

```

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [June 18, 2019, 7:25pm UTC](https://discourse.julialang.org/t/generic-function-to-call-arbitrary-julia-function-and-its-arguments/25407/7 "2019-06-18T19:25:59Z")

</div>

thats right, I,ll add the `;` in my original post.
