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

collect() is inconsistent with Base.Dict #146

Closed
josia-pool opened this issue Jul 30, 2024 · 2 comments
Closed

collect() is inconsistent with Base.Dict #146

josia-pool opened this issue Jul 30, 2024 · 2 comments

Comments

@josia-pool
Copy link

When I use collect on a dictionary from base, I get a vector with Key-Value pairs.
But when I use collect on a Dictionaries.Dictionary, I just get a vector of values.

julia> base = Dict("Test1" => 1, "Test2" =>2)
Dict{String, Int64} with 2 entries:
  "Test2" => 2
  "Test1" => 1

julia> other = Dictionaries.Dictionary(base)
2-element Dictionaries.Dictionary{String, Int64}
 "Test2"2
 "Test1"1

julia> collect(base)
2-element Vector{Pair{String, Int64}}:
 "Test2" => 2
 "Test1" => 1

julia> collect(other)
2-element Vector{Int64}:
 2
 1

Is there a reason for this behavior? Is there possibly an alternative I can use to get similar behavior? I am relatively new to Julia and especially this package.
I would like to iterate over the (collected) dictionary keys and values using numbered indices since i use the same index for other vectors.

@theogf
Copy link
Collaborator

theogf commented Jul 30, 2024

One of the purposes of Dictionaries is to be able to iterate on Dictionary (which is not currently possible: try map(dict) or f.(dict)).
The decision was made that the iteration in Dictionary is done on the values. So collect naturally return the collected iterator i.e. a vector of the values.
You can get a more similar behavior by calling pairs :

julia> collect(pairs(base))
2-element Vector{Pair{String, Int64}}:
 "Test2" => 2
 "Test1" => 1

julia> collect(pairs(other))
2-element Vector{Pair{String, Int64}}:
 "Test2" => 2
 "Test1" => 1

@josia-pool
Copy link
Author

@theogf Thanks for the explanation, that makes sense then.

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

No branches or pull requests

2 participants