Think of something like R’s fivenum function (that returns the min, max, median, and upper/lower quartiles of a data vector). Now, instead of calculating all five numbers, say I wanted to be able to tell the function to just give me a particular subset of those statistics.
Is there a preferred or “Julian” way of handling stuff like this? The obvious (to me) thing to do would be to pass an argument to the function saying which statistics to calculate, and using a bunch of if…ifelse statements, checking each statistic against the arguments to determine whether to calculate it.
However, I’m trying to make this performant, and wasn’t sure whether a whole mess of if statements was a good idea. If nothing else, it makes for a very long and messy function.
An alternative that came to mind was something like the following (I want the results in a vector):
a(x) = [minimum(x), maximum(x), median(x)]
That works fine for me, and it keeps the code nice and clean, but it means hand-coding/constructing the function, and makes it harder for other users, who might be less comfortable writing code. It’s not the end of the world, but ideally, it’d be nice to do something like:
stats = ["maximum", "minimum"]
results = fivenum(x, stats)
especially if the resulting function is still performant.
To do something like that, I am stuck using a whole bunch of if statements? Is there a better way to (essentially) generate and call an array of expressions on a given input? In my use case, the “function of functions” is going to be defined once at runtime, and called many, many times as part of a larger MCMC algorithm.
I’m just learning Julia, so apologies if my questions are malformed, or I’m missing something obvious (I know R and a bit of C++). I’m guessing this is the sort of thing macros might be used for, but I frankly have trouble wrapping my head around macros in Julia…
Thanks!