Skip to content

Commit ca157a1

Browse files
authored
do not drop repeat keys in Accumulate(::Pairs...) (#861)
1 parent e3fa7b6 commit ca157a1

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/accumulator.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ end
1414

1515
Accumulator{T, V}() where {T, V} = Accumulator{T, V}(Dict{T, V}())
1616
Accumulator(map::AbstractDict) = Accumulator(Dict(map))
17-
Accumulator(ps::Pair...) = Accumulator(Dict(ps))
17+
function Accumulator(p::Pair, ps::Pair...)
18+
a = Accumulator(Dict(p))
19+
for (k,v) in ps
20+
inc!(a, k, v)
21+
end
22+
a
23+
end
1824

1925
counter(T::Type) = Accumulator{T, Int}()
2026
counter(dct::AbstractDict{T, V}) where {T, V<:Integer} = Accumulator{T, V}(Dict(dct))

test/test_accumulator.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
@test sum(ct) == 6
5252
end
5353

54-
@testset "From Pairs" begin
54+
@testset "From Pairs" begin
5555
acc = Accumulator("a" => 2, "b" => 3, "c" => 1)
5656
@test isa(acc,Accumulator{String,Int})
5757
@test haskey(acc,"a")
@@ -62,6 +62,15 @@
6262
@test acc["c"] == 1
6363
end
6464

65+
@testset "From Pairs with repeats" begin
66+
acc = Accumulator("a" => 2, "b" => 3, "b" => 1)
67+
@test isa(acc,Accumulator{String,Int})
68+
@test haskey(acc,"a")
69+
@test haskey(acc,"b")
70+
@test acc["a"] == 2
71+
@test acc["b"] == 4
72+
end
73+
6574
@testset "From Vector" begin
6675
ct2 = counter(["a", "a", "b", "b", "a", "c", "c"])
6776
@test isa(ct2, Accumulator{String,Int})

0 commit comments

Comments
 (0)