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

"view objects" for keys(dict) and values(dict) #2347

Closed
daviddelaat opened this issue Feb 18, 2013 · 7 comments
Closed

"view objects" for keys(dict) and values(dict) #2347

daviddelaat opened this issue Feb 18, 2013 · 7 comments
Labels
breaking This change will break code

Comments

@daviddelaat
Copy link
Contributor

Currently keys(dict) and values(dict) are Arrays in Julia.

In Python 2 this was also the case, but in Python 3 this was changed to returning "view objects", see: http://docs.python.org/3.0/library/stdtypes.html#dict.keys
I think they got the idea from the Java collections framework.

Personally I like this a lot, what are your opinions about this for Julia?

@pao
Copy link
Member

pao commented Feb 18, 2013

I'm conservatively applying the "breaking" tag, so please remove it if there's no way anyone could be relying on details of the current behavior.

@StefanKarpinski
Copy link
Member

No, this seems breaking, therefore 0.2. As long as the view is read-only it seems reasonable, but in that case, you can't do things like sort! the view.

@JeffBezanson
Copy link
Member

We could add this easily (yet another use of an immutable type, too), and get arrays of keys with collect(keys(dict)). Seems like a good idea.

@JeffBezanson
Copy link
Member

I implemented this and will push it soon. Then contains should be used to see if an element is in a collection, and has only checks for keys in dictionaries, defined as has(d,k) = contains(keys(d),k). Perhaps it should be called haskey.

@kmsquire
Copy link
Member

Will (or can you make) contains(d, (k,v)) work?

In working on OrderedDicts (which I will resubmit soon), I have alternated between treating key-value pairs as 2-tuples or their own DictItem type. DictItem seems more performant, but from a user's perspective, being able to treat key-value pairs as tuples is useful.

@daviddelaat
Copy link
Contributor Author

Thanks Jeff for working on this.

I think haskey is a good idea, the has function name is rather confusing.

@JeffBezanson
Copy link
Member

Yes contains(d,(k,v)) should work, and I'm including a change to make it faster for dicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking This change will break code
Projects
None yet
Development

No branches or pull requests

5 participants