Skip to content

Commit

Permalink
add is_mergeable, remove fallback _merge! method
Browse files Browse the repository at this point in the history
  • Loading branch information
joshday committed May 21, 2024
1 parent a217f23 commit 2283f61
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/OnlineStatsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ function Base.merge!(o::OnlineStat, o2::OnlineStat)
(nobs(o) > 0 || nobs(o2) > 0) && _merge!(o, o2)
o
end
_merge!(o, o2) = error("Merging $(name(o2)) into $(name(o)) is not defined.")

Base.merge(o::OnlineStat, o2::OnlineStat) = merge!(copy(o), o2)

Base.empty!(o::OnlineStat) = error("$(typeof(o)) has no `Base.empty!` method.")

is_mergeable(o::T) where {T <: OnlineStat} = hasmethod(_merge!, Tuple{T,T})

#-----------------------------------------------------------------------# Base.show
function Base.show(io::IO, o::OnlineStat)
print(io, name(o, false, false))
Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using OnlineStatsBase, OrderedCollections, StatsBase, Statistics, Dates, Test

import OnlineStatsBase: _fit!, _merge!
import OnlineStatsBase: _fit!, _merge!, is_mergeable

O = OnlineStatsBase

Expand All @@ -24,6 +24,9 @@ O = OnlineStatsBase
@test collect(OnlineStatsBase.neighbors([1,3,5])) == [(1,3), (3,5)]
@test isnan(value(fit!(Variance(), NaN)))

@test is_mergeable(Mean()) == true
@test is_mergeable(CircBuff(Int, 5)) == false

@testset "name" begin
name = OnlineStatsBase.name
o = Mean()
Expand Down

0 comments on commit 2283f61

Please sign in to comment.