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

Single naming convention for functions returning indices in 2.0 #50003

Open
jariji opened this issue May 30, 2023 · 4 comments
Open

Single naming convention for functions returning indices in 2.0 #50003

jariji opened this issue May 30, 2023 · 4 comments
Labels
breaking This change will break code search & find The find* family of functions
Milestone

Comments

@jariji
Copy link
Contributor

jariji commented May 30, 2023

There are multiple naming conventions for search functions

  • findfirst, findall return indices
  • findmin/findmax return (value, key) pairs (who ever heard of a (value, key) pair?)
  • searchsorted{,first,last} return indices
  • indmin/indmax (PR) return indices

I suggest find as a "returns indices" convention in 2.0.

@Seelengrab
Copy link
Contributor

@jakobnissen jakobnissen added this to the 2.0 milestone May 31, 2023
@jakobnissen jakobnissen added the breaking This change will break code label May 31, 2023
@nalimilan nalimilan added the search & find The find* family of functions label Jun 5, 2023
@nalimilan
Copy link
Member

+1

See previous discussions at #10593 and the Search & Find Julep. The problems raised here are indeed the parts of the Julep that didn't end up being implemented in Julia 1.0. In particular, #25414 is about searchsorted*, #24865 about findmin/findmax.

@jariji
Copy link
Contributor Author

jariji commented Jul 30, 2023

Edit: This comment doesn't make sense.

findfirst takes a function as the first argument but searchsorted takes the function as by=, like sort. It would be nice to not have different APIs for essentially similar functions. I think that would mean choosing either do or by= for both find* and sort*, but that might not be worth it.

@aplavin
Copy link
Contributor

aplavin commented Jul 30, 2023

searchsorted's by is not really equivalent to findfirst's first argument (needle).
It would be nice to support predicates in searchsorted, like searchsortedfirst(≥(x), xs) (currently it's searchsortedfirst(xs, x)), searchsortedfirst(>(x), xs), searchsortedfirst(==(x), xs), searchsortedfirst(∈(x:y), xs). These variants are arguably as useful as ≥(x), but currently require careful bookkeeping from the user's part.

As we are talking about major changes anyway, searchsorted would be more composable as findfirst(≥(x), Sorted(xs)) with Sorted being a simple wrapper. This way, libraries would just use findfirst and it will work with arbitrary collections (linear search) and sorted collections (binary search).

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 search & find The find* family of functions
Projects
None yet
Development

No branches or pull requests

5 participants