-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 predicate-parameterized search functions to List: indexOfMatch/lastIndexOfMatch #3711
Comments
Update bug description. Wooops. Changed the title to: "Let the element we're looking for be the receiver in == calls in indexOf/lastIndexOf". |
It's probably better that way, but mainly for performance reasons - it means that it's the same equals method that is called a lot of times, instead of potentially different ones called once each. If the equality operator is done correctly, it should be symmetric, so it shouldn't matter which one is used. If the equality operator on your object cheats, and really just tries to match another object in some fancy way, you should search using a predicate instead (which we should make sure is also possible). I actually slightly prefer using the existing objects' equality check in order to discourage such trickery. I.e., your example is a bad example. Positively naughty! So I would suggest that List<T> gets |
This comment was originally written by @seaneagan whichever one is the receiver, it should be consistent with Collection#contains (issue #1030). Instead of List#indexOfMatch/indexOfLastMatch I would prefer Collection#find: E find(bool predicate(E item)); |
This comment was originally written by @seaneagan I filed Collection#find as issue #3948 |
It is consistent with Collection.contains now. We generally let the collection determine if it has something equal to the argument of contains, indexOf (and if we don't, we should fix that). |
This comment was originally written by @seaneagan If we had List.pairs (see issue #3948) and Iterable.find (issue #7088), then we probably wouldn't need indexOfMatch/indexOfLastMatch since then instead of: list.indexOfMatch(test) one could do: list.pairs.find((pair) => test(pair.value)).key; ... which also allows one to find both the matching index and value with a single iteration of the list. |
We are not going to change the order of the operands of ==. Removed Type-Defect label. |
Removed this from the 1.6 milestone. |
Removed Oldschool-Milestone-1.6 label. |
Closing as dupe of #30275. |
When we call indexOf/lastIndexOf on our list implementations, we let the elements in the list dictate if they are equals to the "thing" we're searching for. I find that a bit counterintuitive. I'd prefer if we always asked the thing we're searching for if it feels like it's equal to the elements like this:
The text was updated successfully, but these errors were encountered: