Skip to content

Commit

Permalink
Bring back function between varname and optic (#95)
Browse files Browse the repository at this point in the history
* add `∘` and `⨟` function between varname and optic

* Apply suggestions from code review

Co-authored-by: David Widmann <[email protected]>

---------

Co-authored-by: David Widmann <[email protected]>
  • Loading branch information
sunxd3 and devmotion authored Apr 9, 2024
1 parent 9d35b63 commit a80a0f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ function Accessors.set(obj, vn::VarName{sym}, value) where {sym}
return Accessors.set(obj, PropertyLens{sym}() getoptic(vn), value)
end

# Allow compositions with optic.
function Base.:(optic::ALLOWED_OPTICS, vn::VarName{sym,<:ALLOWED_OPTICS}) where {sym}
return VarName{sym}(optic getoptic(vn))
end

Base.hash(vn::VarName, h::UInt) = hash((getsym(vn), getoptic(vn)), h)
function Base.:(==)(x::VarName, y::VarName)
Expand Down Expand Up @@ -631,8 +635,8 @@ function varname(expr::Expr, concretize=Accessors.need_dynamic_optic(expr))
if concretize
return :(
$(AbstractPPL.VarName){$sym}(
$(AbstractPPL.concretize)($optics, $sym_escaped)
)
$(AbstractPPL.concretize)($optics, $sym_escaped)
)
)
elseif Accessors.need_dynamic_optic(expr)
error("Variable name `$(expr)` is dynamic and requires concretization!")
Expand Down Expand Up @@ -687,7 +691,7 @@ function _parse_obj_optics(ex)
else
throw(ArgumentError(
string("Error while parsing :($ex). Second argument to `getproperty` can only be",
"a `Symbol` or `String` literal, received `$property` instead.")
"a `Symbol` or `String` literal, received `$property` instead.")
))
end
else
Expand Down
7 changes: 6 additions & 1 deletion test/varname.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using OffsetArrays
using AbstractPPL: , , , ,

using AbstractPPL: Accessors
using AbstractPPL.Accessors: IndexLens, PropertyLens
using AbstractPPL.Accessors: IndexLens, PropertyLens,

macro test_strict_subsumption(x, y)
quote
Expand Down Expand Up @@ -50,6 +50,11 @@ end
@test test_equal(@varname(x.a[1:end, end][:], true), @varname(x.a[1:3,2][1:3]))
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])
end

@testset "get & set" begin
x = (a = [1.0 2.0; 3.0 4.0; 5.0 6.0], b = 1.0);
@test get(x, @varname(a[1, 2])) == 2.0
Expand Down

0 comments on commit a80a0f2

Please sign in to comment.