Skip to content

Commit

Permalink
Merge pull request #37 from invenia/mjp/equality-with-wrapped-object
Browse files Browse the repository at this point in the history
Define `==` between `::KeyedDistribution` and `::Distribution`
  • Loading branch information
mjp98 authored Oct 26, 2022
2 parents dae9805 + dc4c4df commit 3cedd03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KeyedDistributions"
uuid = "2576fb08-064d-4cab-b15d-8dda7fcb9a6d"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.9"
version = "0.2.0"

[deps]
AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f"
Expand Down
9 changes: 9 additions & 0 deletions src/KeyedDistributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,13 @@ function Distributions.insupport(d::KeyedDistribution{<:Univariate}, x::Real)
return insupport(distribution(d), x)
end

# Overload equality comparison between `KeyedT` and underlying `T`
for T in (:Distribution, :Sampleable)
KeyedT = Symbol(:Keyed, T)
@eval begin
Base.:(==)(kd::$KeyedT, d::$T) = distribution(kd) == d
Base.:(==)(d::$T, kd::$KeyedT) = kd == d
end
end

end
14 changes: 14 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,18 @@ using Test
@test dimnames(rename(kd, (:name,))) == (:name, )
@test dimnames(rename(kd, :id => :name)) == (:name, )
end

@testset "Equality comparison with wrapped type" begin
d = MvNormal([1.0, 2.0], [1.0, 1.0]);

kd = KeyedDistribution(d, 1:length(d));

@test kd == d
@test d == kd

ks = KeyedSampleable(d, 1:length(d));

@test ks == d
@test d == ks
end
end

2 comments on commit 3cedd03

@mjp98
Copy link
Member Author

@mjp98 mjp98 commented on 3cedd03 Oct 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/71162

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 3cedd03f9d3aa6f40c089557bffe016efaa5b256
git push origin v0.2.0

Please sign in to comment.