Skip to content

Commit

Permalink
Ignore identity optic when composing (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxd3 authored Apr 12, 2024
1 parent 29cdd41 commit 6fbeffd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 6fbeffd

@sunxd3
Copy link
Member Author

@sunxd3 sunxd3 commented on 6fbeffd Apr 12, 2024

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/104783

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.8.3 -m "<description of version>" 6fbeffde547e948bad166798192f6b59805163c6
git push origin v0.8.3

Please sign in to comment.