diff --git a/base/statistics.jl b/base/statistics.jl index 3a75670d..a5310c3f 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -16,18 +16,20 @@ julia> mean([√1, √2, √3]) ``` """ function mean(f::Callable, iterable) - state = start(iterable) - if done(iterable, state) + y = iterate(iterable) + if y == nothing throw(ArgumentError("mean of empty collection undefined: $(repr(iterable))")) end count = 1 - value, state = next(iterable, state) + value, state = y f_value = f(value) total = reduce_first(add_sum, f_value) - while !done(iterable, state) - value, state = next(iterable, state) + y = iterate(iterable, state) + while y !== nothing + value, state = y total += f(value) count += 1 + y = iterate(iterable, state) end return total/count end @@ -86,19 +88,21 @@ realXcY(x::Complex, y::Complex) = real(x)*real(y) + imag(x)*imag(y) var(iterable; corrected::Bool=true, mean=nothing) = _var(iterable, corrected, mean) function _var(iterable, corrected::Bool, mean) - state = start(iterable) - if done(iterable, state) + y = iterate(iterable) + if y === nothing throw(ArgumentError("variance of empty collection undefined: $(repr(iterable))")) end count = 1 - value, state = next(iterable, state) + value, state = y + y = iterate(iterable, state) if mean === nothing # Use Welford algorithm as seen in (among other places) # Knuth's TAOCP, Vol 2, page 232, 3rd edition. M = value / 1 S = real(zero(M)) - while !done(iterable, state) - value, state = next(iterable, state) + while y !== nothing + value, state = y + y = iterate(iterable, state) count += 1 new_M = M + (value - M) / count S = S + realXcY(value - M, value - new_M) @@ -112,8 +116,9 @@ function _var(iterable, corrected::Bool, mean) # Department of Computer Science, Stanford University, # because user can provide mean value that is different to mean(iterable) sum2 = abs2(value - mean::Number) - while !done(iterable, state) - value, state = next(iterable, state) + while y !== nothing + value, state = y + y = iterate(iterable, state) count += 1 sum2 += abs2(value - mean) end