Skip to content

Commit be77c36

Browse files
committed
Add push!(::VarNamedVector, ::Pair)
1 parent 30252dc commit be77c36

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

docs/src/internals/varinfo.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ To ensure that `VarInfo` is simple and intuitive to work with, we want `VarInfo`
2525
- `haskey(::Dict)`: check if a particular `VarName` is present in `metadata`.
2626
- `getindex(::Dict, ::VarName)`: return the realization corresponding to a particular `VarName`.
2727
- `setindex!(::Dict, val, ::VarName)`: set the realization corresponding to a particular `VarName`.
28+
- `push!(::Dict, ::Pair)`: add a new key-value pair to the container.
2829
- `delete!(::Dict, ::VarName)`: delete the realization corresponding to a particular `VarName`.
2930
- `empty!(::Dict)`: delete all realizations in `metadata`.
3031
- `merge(::Dict, ::Dict)`: merge two `metadata` structures according to similar rules as `Dict`.
@@ -41,7 +42,7 @@ To ensure that `VarInfo` is simple and intuitive to work with, we want `VarInfo`
4142

4243
We also want some additional methods that are *not* part of the `Dict` or `Vector` interface:
4344

44-
- `push!(container, ::VarName, value[, transform])`: add a new element to the container, _but_ for this we also need the `VarName` to associate to the new `value`, so the semantics are different from `push!` for a `Vector`.
45+
- `push!(container, varname::VarName, value[, transform])`: add a new element to the container, but with an optional transformation that has been applied to `value`, and should be reverted when returning `container[varname]`. One can also provide a `Pair` instead of a `VarName` and a `value`.
4546

4647
- `update!(container, ::VarName, value[, transform])`: similar to `push!` but if the `VarName` is already present in the container, then we update the corresponding value instead of adding a new element.
4748

src/varnamedvector.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ _compose_no_identity(::typeof(identity), ::typeof(identity)) = identity
702702

703703
"""
704704
push!(vnv::VarNamedVector, vn::VarName, val[, transform])
705+
push!(vnv::VarNamedVector, vn => val[, transform])
705706
706707
Add a variable with given value to `vnv`.
707708
@@ -729,6 +730,11 @@ function Base.push!(vnv::VarNamedVector, vn::VarName, val, transform=identity)
729730
return nothing
730731
end
731732

733+
function Base.push!(vnv::VarNamedVector, pair, transform=identity)
734+
vn, val = pair
735+
return push!(vnv, vn, val, transform)
736+
end
737+
732738
# TODO(mhauru) The gidset and num_produce arguments are used by the old Gibbs sampler.
733739
# Remove this method as soon as possible.
734740
function Base.push!(vnv::VarNamedVector, vn, val, dist, gidset, num_produce)

0 commit comments

Comments
 (0)