I was trying to understand why Statistics.jl has both var and varm functions (since var already accepts a mean parameter). Doing @less var([1,2,3]) I see:
If the the first argument in something is nothing, the result is
julia> something(nothing, 2)
2
if the first argument in something is not nothing, the result is:
julia> something(3, 2)
3
So it appears that the mean is not recalculated if it is provided.
I’m not sure why there are two methods with the same functionality. My guess is that varm might be deprecated at some point, or might eventually be for internal use only. Good question.
Since something is a function and not control flow, I think it must evaluate both arguments, even if it then chooses to return the first. So to me it looks like it still recalculates the mean, which is surprising.
Yes, it’s quite clear that var(v) and var(v; mean=meanv) have essentially identical timings, whereas the difference between their times and the time for varm(v, meanv) is essentially exactly the time it takes to calculate mean(v).
I’m sure a PR to Statistics.jl rectifying this would be quite welcome.
I assumed that something contained control flow logic to prevent unnecessary evaluations. Good catch!
Edit: Oh man. I just realized that control flow doesn’t even matter because the mean has to be calculated before it’s passed to something. Perhaps that’s why it was lurking there for years.