diff --git a/src/accumulator.jl b/src/accumulator.jl index 19065d52c..feaed0a39 100644 --- a/src/accumulator.jl +++ b/src/accumulator.jl @@ -14,7 +14,13 @@ end Accumulator{T, V}() where {T, V} = Accumulator{T, V}(Dict{T, V}()) Accumulator(map::AbstractDict) = Accumulator(Dict(map)) -Accumulator(ps::Pair...) = Accumulator(Dict(ps)) +function Accumulator(p::Pair, ps::Pair...) + a = Accumulator(Dict(p)) + for (k,v) in ps + inc!(a, k, v) + end + a +end counter(T::Type) = Accumulator{T, Int}() counter(dct::AbstractDict{T, V}) where {T, V<:Integer} = Accumulator{T, V}(Dict(dct)) diff --git a/test/test_accumulator.jl b/test/test_accumulator.jl index d6ba70d15..173553fc8 100644 --- a/test/test_accumulator.jl +++ b/test/test_accumulator.jl @@ -51,7 +51,7 @@ @test sum(ct) == 6 end - @testset "From Pairs" begin + @testset "From Pairs" begin acc = Accumulator("a" => 2, "b" => 3, "c" => 1) @test isa(acc,Accumulator{String,Int}) @test haskey(acc,"a") @@ -62,6 +62,15 @@ @test acc["c"] == 1 end + @testset "From Pairs with repeats" begin + acc = Accumulator("a" => 2, "b" => 3, "b" => 1) + @test isa(acc,Accumulator{String,Int}) + @test haskey(acc,"a") + @test haskey(acc,"b") + @test acc["a"] == 2 + @test acc["b"] == 4 + end + @testset "From Vector" begin ct2 = counter(["a", "a", "b", "b", "a", "c", "c"]) @test isa(ct2, Accumulator{String,Int})