@@ -751,199 +751,6 @@ function unflatten(original::AbstractDict, x::AbstractVector)
751751    return  D (zip (keys (original), unflatten (collect (values (original)), x)))
752752end 
753753
754- #  TODO : Move `getvalue` and `hasvalue` to AbstractPPL.jl.
755- """ 
756-     getvalue(vals, vn::VarName) 
757- 
758- Return the value(s) in `vals` represented by `vn`. 
759- 
760- Note that this method is different from `getindex`. See examples below. 
761- 
762- # Examples 
763- 
764- For `NamedTuple`: 
765- 
766- ```jldoctest 
767- julia> vals = (x = [1.0],); 
768- 
769- julia> DynamicPPL.getvalue(vals, @varname(x)) # same as `getindex` 
770- 1-element Vector{Float64}: 
771-  1.0 
772- 
773- julia> DynamicPPL.getvalue(vals, @varname(x[1])) # different from `getindex` 
774- 1.0 
775- 
776- julia> DynamicPPL.getvalue(vals, @varname(x[2])) 
777- ERROR: BoundsError: attempt to access 1-element Vector{Float64} at index [2] 
778- [...] 
779- ``` 
780- 
781- For `AbstractDict`: 
782- 
783- ```jldoctest 
784- julia> vals = Dict(@varname(x) => [1.0]); 
785- 
786- julia> DynamicPPL.getvalue(vals, @varname(x)) # same as `getindex` 
787- 1-element Vector{Float64}: 
788-  1.0 
789- 
790- julia> DynamicPPL.getvalue(vals, @varname(x[1])) # different from `getindex` 
791- 1.0 
792- 
793- julia> DynamicPPL.getvalue(vals, @varname(x[2])) 
794- ERROR: BoundsError: attempt to access 1-element Vector{Float64} at index [2] 
795- [...] 
796- ``` 
797- 
798- In the `AbstractDict` case we can also have keys such as `v[1]`: 
799- 
800- ```jldoctest 
801- julia> vals = Dict(@varname(x[1]) => [1.0,]); 
802- 
803- julia> DynamicPPL.getvalue(vals, @varname(x[1])) # same as `getindex` 
804- 1-element Vector{Float64}: 
805-  1.0 
806- 
807- julia> DynamicPPL.getvalue(vals, @varname(x[1][1])) # different from `getindex` 
808- 1.0 
809- 
810- julia> DynamicPPL.getvalue(vals, @varname(x[1][2])) 
811- ERROR: BoundsError: attempt to access 1-element Vector{Float64} at index [2] 
812- [...] 
813- 
814- julia> DynamicPPL.getvalue(vals, @varname(x[2][1])) 
815- ERROR: KeyError: key x[2][1] not found 
816- [...] 
817- ``` 
818- """ 
819- getvalue (vals:: NamedTuple , vn:: VarName ) =  get (vals, vn)
820- getvalue (vals:: AbstractDict , vn:: VarName ) =  nested_getindex (vals, vn)
821- 
822- """ 
823-     hasvalue(vals, vn::VarName) 
824- 
825- Determine whether `vals` has a mapping for a given `vn`, as compatible with [`getvalue`](@ref). 
826- 
827- # Examples 
828- With `x` as a `NamedTuple`: 
829- 
830- ```jldoctest 
831- julia> DynamicPPL.hasvalue((x = 1.0, ), @varname(x)) 
832- true 
833- 
834- julia> DynamicPPL.hasvalue((x = 1.0, ), @varname(x[1])) 
835- false 
836- 
837- julia> DynamicPPL.hasvalue((x = [1.0],), @varname(x)) 
838- true 
839- 
840- julia> DynamicPPL.hasvalue((x = [1.0],), @varname(x[1])) 
841- true 
842- 
843- julia> DynamicPPL.hasvalue((x = [1.0],), @varname(x[2])) 
844- false 
845- ``` 
846- 
847- With `x` as a `AbstractDict`: 
848- 
849- ```jldoctest 
850- julia> DynamicPPL.hasvalue(Dict(@varname(x) => 1.0, ), @varname(x)) 
851- true 
852- 
853- julia> DynamicPPL.hasvalue(Dict(@varname(x) => 1.0, ), @varname(x[1])) 
854- false 
855- 
856- julia> DynamicPPL.hasvalue(Dict(@varname(x) => [1.0]), @varname(x)) 
857- true 
858- 
859- julia> DynamicPPL.hasvalue(Dict(@varname(x) => [1.0]), @varname(x[1])) 
860- true 
861- 
862- julia> DynamicPPL.hasvalue(Dict(@varname(x) => [1.0]), @varname(x[2])) 
863- false 
864- ``` 
865- 
866- In the `AbstractDict` case we can also have keys such as `v[1]`: 
867- 
868- ```jldoctest 
869- julia> vals = Dict(@varname(x[1]) => [1.0,]); 
870- 
871- julia> DynamicPPL.hasvalue(vals, @varname(x[1])) # same as `haskey` 
872- true 
873- 
874- julia> DynamicPPL.hasvalue(vals, @varname(x[1][1])) # different from `haskey` 
875- true 
876- 
877- julia> DynamicPPL.hasvalue(vals, @varname(x[1][2])) 
878- false 
879- 
880- julia> DynamicPPL.hasvalue(vals, @varname(x[2][1])) 
881- false 
882- ``` 
883- """ 
884- function  hasvalue (vals:: NamedTuple , vn:: VarName{sym} ) where  {sym}
885-     #  LHS: Ensure that `nt` indeed has the property we want.
886-     #  RHS: Ensure that the optic can view into `nt`.
887-     return  haskey (vals, sym) &&  canview (getoptic (vn), getproperty (vals, sym))
888- end 
889- 
890- #  For `dictlike` we need to check wether `vn` is "immediately" present, or
891- #  if some ancestor of `vn` is present in `dictlike`.
892- function  hasvalue (vals:: AbstractDict , vn:: VarName )
893-     #  First we check if `vn` is present as is.
894-     haskey (vals, vn) &&  return  true 
895- 
896-     #  If `vn` is not present, we check any parent-varnames by attempting
897-     #  to split the optic into the key / `parent` and the extraction optic / `child`.
898-     #  If `issuccess` is `true`, we found such a split, and hence `vn` is present.
899-     parent, child, issuccess =  splitoptic (getoptic (vn)) do  optic
900-         o =  optic ===  nothing  ?  identity :  optic
901-         haskey (vals, VarName {getsym(vn)} (o))
902-     end 
903-     #  When combined with `VarInfo`, `nothing` is equivalent to `identity`.
904-     keyoptic =  parent ===  nothing  ?  identity :  parent
905- 
906-     #  Return early if no such split could be found.
907-     issuccess ||  return  false 
908- 
909-     #  At this point we just need to check that we `canview` the value.
910-     value =  vals[VarName {getsym(vn)} (keyoptic)]
911- 
912-     return  canview (child, value)
913- end 
914- 
915- """ 
916-     nested_getindex(values::AbstractDict, vn::VarName) 
917- 
918- Return value corresponding to `vn` in `values` by also looking 
919- in the the actual values of the dict. 
920- """ 
921- function  nested_getindex (values:: AbstractDict , vn:: VarName )
922-     maybeval =  get (values, vn, nothing )
923-     if  maybeval != =  nothing 
924-         return  maybeval
925-     end 
926- 
927-     #  Split the optic into the key / `parent` and the extraction optic / `child`.
928-     parent, child, issuccess =  splitoptic (getoptic (vn)) do  optic
929-         o =  optic ===  nothing  ?  identity :  optic
930-         haskey (values, VarName {getsym(vn)} (o))
931-     end 
932-     #  When combined with `VarInfo`, `nothing` is equivalent to `identity`.
933-     keyoptic =  parent ===  nothing  ?  identity :  parent
934- 
935-     #  If we found a valid split, then we can extract the value.
936-     if  ! issuccess
937-         #  At this point we just throw an error since the key could not be found.
938-         throw (KeyError (vn))
939-     end 
940- 
941-     #  TODO : Should we also check that we `canview` the extracted `value`
942-     #  rather than just let it fail upon `get` call?
943-     value =  values[VarName {getsym(vn)} (keyoptic)]
944-     return  child (value)
945- end 
946- 
947754""" 
948755    update_values!!(vi::AbstractVarInfo, vals::NamedTuple, vns) 
949756
0 commit comments