From 6fbeffde547e948bad166798192f6b59805163c6 Mon Sep 17 00:00:00 2001 From: Xianda Sun <5433119+sunxd3@users.noreply.github.com> Date: Fri, 12 Apr 2024 11:57:21 +0100 Subject: [PATCH] Ignore `identity` optic when composing (#96) --- Project.toml | 2 +- src/varname.jl | 9 ++++++++- test/varname.jl | 7 +++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index f4f73c8..e929d94 100644 --- a/Project.toml +++ b/Project.toml @@ -3,7 +3,7 @@ uuid = "7a57a42e-76ec-4ea3-a279-07e840d6d9cf" keywords = ["probablistic programming"] license = "MIT" desc = "Common interfaces for probabilistic programming" -version = "0.8.2" +version = "0.8.3" [deps] AbstractMCMC = "80f14c24-f653-4e6a-9b94-39d6b0f70001" diff --git a/src/varname.jl b/src/varname.jl index 0f470a1..05d20b6 100644 --- a/src/varname.jl +++ b/src/varname.jl @@ -150,7 +150,14 @@ end # Allow compositions with optic. function Base.:∘(optic::ALLOWED_OPTICS, vn::VarName{sym,<:ALLOWED_OPTICS}) where {sym} - return VarName{sym}(optic ∘ getoptic(vn)) + vn_optic = getoptic(vn) + if vn_optic == identity + return VarName{sym}(optic) + elseif optic == identity + return vn + else + return VarName{sym}(optic ∘ vn_optic) + end end Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getoptic(vn)), h) diff --git a/test/varname.jl b/test/varname.jl index a26e346..d0e3de7 100644 --- a/test/varname.jl +++ b/test/varname.jl @@ -53,6 +53,13 @@ end @testset "compose and opcompose" begin @test IndexLens(1) ∘ @varname(x.a) == @varname(x.a[1]) @test @varname(x.a) ⨟ IndexLens(1) == @varname(x.a[1]) + + @test @varname(x) ⨟ identity == @varname(x) + @test identity ∘ @varname(x) == @varname(x) + @test @varname(x.a) ⨟ identity == @varname(x.a) + @test identity ∘ @varname(x.a) == @varname(x.a) + @test @varname(x[1].b) ⨟ identity == @varname(x[1].b) + @test identity ∘ @varname(x[1].b) == @varname(x[1].b) end @testset "get & set" begin