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

first(_, n), last(_, n) for Dictionaries #145

Open
jariji opened this issue Jul 28, 2024 · 2 comments
Open

first(_, n), last(_, n) for Dictionaries #145

jariji opened this issue Jul 28, 2024 · 2 comments

Comments

@jariji
Copy link

jariji commented Jul 28, 2024

  last(itr, n::Integer)


  Get the last n elements of the iterable collection itr, or fewer elements if itr is not long enough.

Currently last(d, n) returns a Vector but it could return a Dictionary. What do you think?

@jariji
Copy link
Author

jariji commented Jul 28, 2024

For example, to get the top 5 values of a dictionary, I sort the dictionary by value and then take the last 5 entries. While retaining the keys as labels.

@jariji
Copy link
Author

jariji commented Jul 28, 2024

Maybe like this

Base.first(d::AbstractDictionary, n::Integer) = getindices(d, Indices(Iterators.take(keys(d), n)))

Base.last(d::AbstractDictionary, n::Integer) = getindices(d, reverse(Indices(Iterators.take(reverse(keys(d)), n))))

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

1 participant