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 functions like findonly, uniqueonly, ... #54118

Open
aplavin opened this issue Apr 17, 2024 · 2 comments
Open

Add functions like findonly, uniqueonly, ... #54118

aplavin opened this issue Apr 17, 2024 · 2 comments
Labels
search & find The find* family of functions

Comments

@aplavin
Copy link
Contributor

aplavin commented Apr 17, 2024

I'd like to propose adding a bunch of simple functions to Julia:

  • findonly: complements findfirst and findlast, throws if there are multiple matches
  • filterfirst, filteronly, maybe filterlast: same as first/last/only(filter(pred, collection)) but faster
  • uniqueonly: like only(unique(collection)), but faster

These are straightforward to implement, and the motivation to include them is similar to that of only(): mostly for safety and convenience.

@jakobnissen
Copy link
Contributor

In my opinion, findonly, filterfirst, filteronly and filterlast are too trivial to be a good idea. These can straightforwardly be written in terms of lazy operations which are much more generically useful that what's proposed here:

  • findonly -> Iterators.findall + only
  • filterfirst -> Iterators.filter + first
  • filteronly -> Iterators.filter + only
  • filterlast -> Iterators.reverse + Iterators.filter + first

The only issues with these I see are:

  1. We don't currently have Iterators.findall (Implement lazy findall (Iterators.findall, perhaps?) #43737)
  2. It's slightly annoying that the most useful versions of filter, reverse, etc. are in the Iterators namespace.

@aplavin
Copy link
Contributor Author

aplavin commented Apr 18, 2024

findonly -> Iterators.findall + only

Aside from Iterators.findall not existing, it breaks the straightforward symmetry: first - findfirst(A) vs only - only(Iterators.findall(A)).
I can easily see people using findfirst for simplicity when they really need findonly. It's the same as was before introducing only to Julia: many used first instead.

Also, uniqueonly probably cannot be made performant without a dedicated function for it.

It's slightly annoying that the most useful versions of filter, reverse, etc. are in the Iterators namespace.

Idk, the Base ones are definitely the most useful for me. The return types and performance model of eager operations is the more straightforward one in Julia, compared to lazy Iterators.xxx. Eg, doing filter on an array (or table) gives you the same kind of object back – not some generic iterator.

@nalimilan nalimilan added the search & find The find* family of functions label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
search & find The find* family of functions
Projects
None yet
Development

No branches or pull requests

3 participants