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

Add Navigators for Sorted Sequences #317

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

IGJoshua
Copy link
Contributor

@IGJoshua IGJoshua commented Aug 23, 2021

This PR adds support for three new navigators which all navigate to a sorted version of the current sequence.

  • SORTED, a parameter-less navigator which navigates to a transformable view of the current sequence as if it were run through sort
  • sorted, a single-parameter navigator which is the same as SORTED but takes a comparator
  • sorted-by, a dynamic navigator which takes a keypath for the sort key and an optional comparator

Before this PR can be merged, the following steps must be completed by the author:

  • Add tests

This PR implements what was discussed in #316.

@IGJoshua IGJoshua marked this pull request as ready for review August 26, 2021 18:18
Value collection (e.g. collect, collect-one) may not be used in the keypath."
([keypath] (sorted-by keypath compare))
([keypath comparator]
(late-bound-nav [late (late-path keypath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either the naming or implementation here is off. As named, this isn't accepting general paths but only a key. In this case, it's wrong to use late-path and it will be more straightforward/faster to use get rather than compiled-select. Additionally, compiled-select returns a sequence of results which I don't think you want to be comparing against.

An alternative is to have the first arg be an "extractor path", and use compiled-select-one! to extract the key. This would allow for usage where the desired comparator key is nested or transformed, like (sorted-by [:a :b (view -)])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my intention was to have it be a generic full path, what you're calling an extractor path. I thought keypath would be an appropriate term here since in clojure.core/sort-by the argument is called the keyfn. I'll change this to use compiled-select-one!.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this to use compiled-select-one!. Would you recommend I also rename the keypath, or is this enough?

indices (map first sorted)
result (next-fn (map second sorted))
unsorted (sort-by first compare (map vector (concat indices (repeat ##Inf)) result))]
(into (empty structure)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work with lists? The issue with (into '() ...) is it prepends for each element, oftentimes reversing the output order of what you expect. This needs a test at least.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point, I'll add a test for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated this, it has a test and works with lists as well as vectors.

@IGJoshua
Copy link
Contributor Author

Are there any other changes you'd like me to do @nathanmarz before it would be ready for a merge?

@arichiardi
Copy link

arichiardi commented Apr 12, 2023

Oh I was looking for this for a use case I have got, any recommended alternative?

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

3 participants