Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend mean((x, y)) rather than middle((x, y)) #147

Merged
merged 4 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ if !isdefined(Base, :mean)
function mean(f::Number, itr::Number)
f_value = try
f(itr)
catch MethodError
rethrow(ArgumentError("""mean(f, itr) requires a function and an iterable.
Perhaps you meant middle(x, y)?""",))
catch err
if err isa MethodError && err.f === f && err.args == (itr,)
rethrow(ArgumentError("""mean(f, itr) requires a function and an iterable.
Perhaps you meant mean((x, y))?"""))
else
rethrow(err)
end
end
Base.reduce_first(+, f_value)/1
end
Expand Down
12 changes: 9 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,18 @@ end
@test isnan(@inferred mean(Iterators.filter(x -> true, Float64[])))

# using a number as a "function"
@test_throws "ArgumentError: mean(f, itr) requires a function and an iterable.\nPerhaps you meant middle(x, y)" mean(1, 2)
@test_throws "ArgumentError: mean(f, itr) requires a function and an iterable.\nPerhaps you meant mean((x, y))" mean(1, 2)
struct T <: Number
x::Int
end
(t::T)(y) = t.x * y
@test @inferred mean(T(2), 3) === 6.0
(t::T)(y) = t.x == 0 ? t(y, y + 1, y + 2) : t.x * y
@test mean(T(2), 3) === 6.0
err = @test_throws MethodError mean(T(0), 3)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
struct U <: Number
x::Int
end
(t::U)(y) = t.x == 0 ? throw(MethodError(T)) : t.x * y
@test @inferred mean(U(2), 3) === 6.0
end

@testset "mean/median for ranges" begin
Expand Down