Skip to content

Commit

Permalink
add 1-argument dict constructor, from anything that generates pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Nov 22, 2013
1 parent 4fb1082 commit 93274a3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,21 @@ type Dict{K,V} <: Associative{K,V}
new(zeros(Uint8,n), Array(K,n), Array(V,n), 0, 0, identity)
end
function Dict(ks, vs)
# TODO: eventually replace with a call to Dict(zip(ks,vs))
n = length(ks)
h = Dict{K,V}()
for i=1:n
h[ks[i]] = vs[i]
end
return h
end
function Dict(kv)
h = Dict{K,V}()
for (k,v) in kv
h[k] = v
end
return h
end
end
Dict() = Dict{Any,Any}()

Expand All @@ -308,6 +316,8 @@ Dict{K,V}(ks::(K...), vs::(V...)) = Dict{K ,V }(ks, vs)
Dict{K }(ks::(K...), vs::Tuple ) = Dict{K ,Any}(ks, vs)
Dict{V }(ks::Tuple , vs::(V...)) = Dict{Any,V }(ks, vs)

Dict{K,V}(kv::Array{(K,V)}) = Dict{K,V}(kv)

This comment has been minimized.

Copy link
@kmsquire

kmsquire Nov 22, 2013

Member

Should this be AbstractArray?

This comment has been minimized.

Copy link
@JeffBezanson

JeffBezanson Nov 22, 2013

Author Member

Yes.


similar{K,V}(d::Dict{K,V}) = (K=>V)[]

function serialize(s, t::Dict)
Expand Down

0 comments on commit 93274a3

Please sign in to comment.