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

make keys(dict) and values(dict) more useful #20678

Closed
StefanKarpinski opened this issue Feb 19, 2017 · 7 comments
Closed

make keys(dict) and values(dict) more useful #20678

StefanKarpinski opened this issue Feb 19, 2017 · 7 comments
Labels
collections Data structures holding multiple items, e.g. sets

Comments

@StefanKarpinski
Copy link
Member

Currently the very common operation of iterating a Dict's keys in sorted order requires writing the overly verbose sort!(collect(keys(d))). This could be abbreviated to sort(keys(d)) by adding a sort method for KeyIterator, but do we manually add methods for all relevant functions in Base? What about user-defined functions that make sense for general collection-like types? At some point (IIRC) I proposed making KeyIterator and ValueIterator subtypes of AbstractVector, which would make many things "just work". The trouble is that indexing is O(n); however, I believe that something like @JeffBezanson's ordered dict experiment could make indexing O(1).

@StefanKarpinski
Copy link
Member Author

Mutating keys(d) and values(d) could actually potentially make sense – keys(d)[i] = k and values(d)[i] = v could be efficient equivalents of these:

# keys(d)[i] = k
d[k] = pop!(d, collect(d)[i].first)

# values(d)[i] = v
d[collect(d)[i].first] = v

Moreover, if dictionaries are ordered, then writing code like sort!(keys(d)) could be another way of writing sort!(d, by=p->p[1]) and similarly sort!(values(d)) could be equivalent to doing sort!(d, by=p->p[2]), assuming sort! for Dicts is allowed (questionable since indexing Dicts can't be).

@StefanKarpinski StefanKarpinski changed the title make keys(dict) and values(dict) less annoying to work with make keys(dict) and values(dict) more functional Feb 19, 2017
@ararslan
Copy link
Member

Many moons ago I had a PR for a sort fallback that would handle general iterables (#16853), though it was somewhat ill-conceived at the time. The idea was to just collect before sorting.

@ararslan ararslan added the collections Data structures holding multiple items, e.g. sets label Feb 19, 2017
@JeffBezanson JeffBezanson changed the title make keys(dict) and values(dict) more functional make keys(dict) and values(dict) more useful Feb 20, 2017
@JeffBezanson
Copy link
Member

Dup of #10092 (almost exactly 2 years later!) ?

@rfourquet
Copy link
Member

I would often want to call also sort(::Dict) at the REPL, which @ararslan PR would allow.

@ararslan
Copy link
Member

Wouldn't you want sort(::Dict) to give you a SortedDict though? 🙂

@rfourquet
Copy link
Member

Like I said in your PR, I would prefer the predicability of getting an array out. If I wish a SortedDict, SortedDict(::Dict) is simple enough! As for my visual consumption at the REPL, an array of pairs is plenty enough :)

@mbauman
Copy link
Member

mbauman commented Sep 15, 2017

We probably don't need both of these open. Closing in favor of #10092.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

No branches or pull requests

5 participants