Skip to content

Commit 8337376

Browse files
authored
Define custom dict_inverse(d::Base.ImmutableDict) (#44)
1 parent ed40d49 commit 8337376

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Bijections.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ function dict_inverse(d::D) where {D<:AbstractDict}
170170
return inverse_dict_type(D)(reverse.(collect(d)))
171171
end
172172

173+
function dict_inverse(d::Base.ImmutableDict{K,V}) where {K,V}
174+
# ImmutableDict does not have a ImmutableDict{K,V}(pairs...) constructor
175+
# The version of the constructor with type parameters was overlooked in
176+
# https://github.com/JuliaLang/julia/issues/35863
177+
d_inv = Base.ImmutableDict{V,K}()
178+
for (k, v) in reverse.(collect(d))
179+
d_inv = Base.ImmutableDict{V,K}(d_inv, k, v)
180+
end
181+
return d_inv
182+
end
183+
173184
"""
174185
inv(b::Bijection)
175186

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ end
8888
@test length(b) == 2
8989
@test b["one"] == 1
9090
@test b["two"] == 2
91+
92+
# test construction from ImmutableDict
93+
d = Base.ImmutableDict(1 => "one", 2 => "two")
94+
b = Bijection(d)
95+
@test length(b) == 2
96+
@test b[1] == "one"
97+
@test b[2] == "two"
9198
end
9299

93100
# Test inv function

0 commit comments

Comments
 (0)