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 Array.{find_opt,find_map,split,combine} #9582
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Except for the lack of changes entry, nothing to say.
Thanks! Changes entry added. Planning to merge once CI passes. |
Added some tests for good measure. |
For arrays I find it natural to also have versions that return the index at which the element was found: |
A more general function is |
(I think that even |
I was a bit obsessed about matching
The first two avoid an allocation if you are only interested in getting the value (you index the array with the given int, rather than having the tuple index/value being created). |
I think it is apparent now that I opened a can of design worms with my small point. I don't want to make @nojb's life harder than it should be, and maybe we can discuss those in a separate PR (or do nothing at all about these points). This should be fine as long as my proposal and, for example, @dbuenzli's, both have what is currently proposed as a common subset. Do we agree that this is the case? (Daniel, does your different API suggest not having the functions currently proposed?) |
I'm in general not very fond of having a bazillion of different functions but maybe it doesn't hurt to have the minimal set of functions proposed by this PR on the ground of |
@dbuenzli : in your proposal, I guess that |
Whatever functions are being added (and once the discussion has settled, of course), I would argue to add the same functions to |
I think it makes sense to take some time to discuss the API for "find" functions on arrays. Also, by the way, Alain pointed out that I had jumped the gun by getting ready to merge without having received an approval from another maintainer, so let us take a step back and take some time to arrive at a good design. |
The perspective of this function is to be able to restart a search after you found an element so that you can collect all of them in the array (i.e. make it easy to compose invocations to get your own "find_all"). Of course you an always add more toggles to functions but in the end I prefer functions without too many optional features. Note that to limit the search you can do that with
Here again in the end I don't like to add to many toggles to functions. I made a few designs in the past with optional |
In the case of |
Yes. I think this is a great idea, especially if we get functions like drop and take to replace the need for a start_idx.
Isn't that just a |
This adds a lot of allocations to find something.
The control flow is inverted and you can stop before. But yes everything is a fold. |
I gave this some more thought, and I like the following two functions:
(I still haven't decided if having "left"/"right" versions or a What do you think? |
By the way, if you are using arrays chances are that you care about the compact layout it provides. Going through a |
(Sorry I had missed part of the discussion and I see my suggestion is very close to what is proposed in this comment) |
In Batteries we have a type |
OK, according to this, if we add "find" function to
What do you think? Would these functions be worthy of addition, even if not the most general ones? |
Yes, I think they are fine. But then maybe I am being unfair to optional parameters, and we should really consider having them for all functions? (I'm not a very good standard-library-API designer.) (Consistently with |
PR stalled without clear consensus. I'm leaning towards closing it, but I'm wondering if it is worth putting |
I looked at the patch again: personally I find the proposed API (which is exactly the one of List) very reasonable. We diverged when we tried to propose finer-grained APIs for traversing only a sub-array, but if we forget about this, I like the functions that are actually in the PR. They also received an approval from @dbuenzli, which is very high bar for a Stdlib change. So maybe we could decide that we like I generally follow the principle of not approving Stdlib PRs myself. Here we have somewhat of an approval resource starvation, because the most active stdlib maintainer is @nojb, and it's his turn to propose something. Maybe some other maintainer would like to chime in? |
What do we do with this? Anyone willing to voice a vote either way? If no-one chimes in, we will close it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an exception to my own rule, here is an approval stamp.
Thanks! I rebased the PR. |
Both CI errors ( |
What it says on the label.