From 2283f616bb52be23eff20279f79239a3d4becbb6 Mon Sep 17 00:00:00 2001 From: Josh Day Date: Tue, 21 May 2024 10:36:58 -0400 Subject: [PATCH] add is_mergeable, remove fallback _merge! method --- src/OnlineStatsBase.jl | 4 +++- test/runtests.jl | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/OnlineStatsBase.jl b/src/OnlineStatsBase.jl index 124b42a..450e38d 100644 --- a/src/OnlineStatsBase.jl +++ b/src/OnlineStatsBase.jl @@ -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)) diff --git a/test/runtests.jl b/test/runtests.jl index 8f055ba..612245e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,6 @@ using OnlineStatsBase, OrderedCollections, StatsBase, Statistics, Dates, Test -import OnlineStatsBase: _fit!, _merge! +import OnlineStatsBase: _fit!, _merge!, is_mergeable O = OnlineStatsBase @@ -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()