Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Dict constructor from tuple array #4872

Closed
wants to merge 1 commit into from

Conversation

kmsquire
Copy link
Member

Right now, we can get an array of tuples from a Dictionary via collect, but there's no constructor from such an array. This pull request remedies that.

julia> a = [("A",1),("B",2),("C",4)]
3-element Array{(ASCIIString,Int64),1}:
 ("A",1)
 ("B",2)
 ("C",4)

julia> Dict(a)
["B"=>2,"C"=>4,"A"=>1]

julia> typeof(ans)
Dict{ASCIIString,Int64} (constructor with 2 methods)

@JeffBezanson
Copy link
Sponsor Member

Good idea, but it might as well work for any iterator that generates 2-tuples. This is also probably not a good case for zip since there might be a very large number of things to zip.

I would do it like this:

    # inner ctor
    function Dict(kv)
        h = Dict{K,V}()
        for (k,v) in kv
            h[k] = v
        end
        return h
    end
end

Dict(ks, vs) = Dict{Any,Any}(zip(ks,vs))
# etc.

Once we merge @loladiro 's tuples branch zip(x,y) will be as fast as anything.

@JeffBezanson
Copy link
Sponsor Member

added in 93274a3

@kmsquire
Copy link
Member Author

Thanks!

On Fri, Nov 22, 2013 at 1:48 PM, Jeff Bezanson notifications@github.comwrote:

added in 93274a393274a3


Reply to this email directly or view it on GitHubhttps://github.com//pull/4872#issuecomment-29111843
.

@kmsquire kmsquire deleted the kms/dict_from_tuple_array branch December 4, 2013 06:30
@coveralls
Copy link

Coverage Status

Changes Unknown when pulling 56ff0db on kms/dict_from_tuple_array into * on master*.

@jiahao
Copy link
Member

jiahao commented Mar 2, 2015

^ over 40 million seconds late...?

@tkelman
Copy link
Contributor

tkelman commented Mar 2, 2015

We don't even have coveralls turned on for base Julia. If you look at the list of files it's a bunch of ruby having to do with paypal? This has happened before, I think coveralls is hitting some hash collision or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants