<div dir="ltr">Thanks for the advice. Indeed, it was a mistake to implement this before discussing it in mail. This is my first open-source experience, so i guess this will be a valuable experience for my future contributions<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">сб, 20 апр. 2024 г. в 00:12, David Alayachew <<a href="mailto:davidalayachew@gmail.com">davidalayachew@gmail.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:monospace">Hello</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Thanks for sending this email. Your idea makes a lot of sense, but I feel that it only serves a very specific use case. That does not make it bad, but it does make it narrow.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">I already suggested my idea in my email to Rémi, where the index is just another field in the data type being streamed. Doing it this way, we can not only cover your use case, but the use cases of many others. It has a far wider reach for the same level of effort.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">And finally, make it a point to send an email on the mailing list and getting consensus BEFORE making a pull request. I am guilty of this too. But going out of order like this slows down the system and forces reviewers to take extra steps that they wouldn't have to if you had followed protocol.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 19, 2024 at 5:02 PM David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><span style="font-family:monospace"><br>No Rémi, I don't think your idea is the right approach. You are working on the wrong level of abstraction.<br><br>Many users ask requests like this all the time, and what you are suggesting would be even more error-prone than the equivalent for loop or <span class="gmail_default" style="font-family:monospace">the </span>IntStream suggestion that the user above requested. Not to mention that getting it to parallelize would be a task many users are likely to mess up -- either in correctness or in performance.<br><br>I think you would get far more mileage from adding 2 methods on the list interface streamWithIndex() and parallelStreamWithIndex() that would return a Stream<WithIndex<T>>, as opposed to just Stream<T>.<br><br></span><div><span style="font-family:monospace">That way, users are not writing a custom Gatherer each time they want to work with the index. They just have the index be a field in the object. They can work with it the same way they would any other object field.</span></div><div><span style="font-family:monospace"><br></span></div><div style="font-family:monospace" class="gmail_default">Furthermore, doing it this way makes the correct answer obvious. If I need to do something with an index, stream with the index.</div><div style="font-family:monospace" class="gmail_default"><br></div><div style="font-family:monospace" class="gmail_default">On top of that, it significantly enhances readability by making it clear to the reader that, whatever this stream is doing will require use of the index, so watch out for that.<br></div><br><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Apr 19, 2024, 1:47 PM Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div id="m_3409512792581686759m_5463014457698901237m_-4157093933715390893m_9119089240644209637zimbraEditorContainer" style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)"><div>Hello,</div><div>for me, it seems what you want is Collector on Stream which is able to short-circuit,<br></div><div>so you can write<br></div><div> list.stream().collect(Collectors.findFirst(s -> s.contains("o")))<br></div><div>and in reverse<br></div><div> list.reversed().stream().collect(Collectors.findFirst(s -> s.contains("o")))<br></div><div> <br></div><div>Using a Stream here is more general and will work with other collections like a LinkedHashSet for example.<br></div><div>Sadly, there is no way to define a short-circuiting collector :(<br></div><div><br></div><div>You can have a short-circuiting Gatherer like this<br></div><div> <T> Gatherer<T, ?, Integer> findIndex(Predicate<? super T> predicate) {<br> return Gatherer.ofSequential(<br> () -> new Object() { int index; },<br> Integrtor.ofGreedy((state, element, downstream) -> {<br> var index = state.index++;<br> if (predicate.test(element)) {<br> return downstream.push(index);<br> }<br> return true;<br> }));<br> }<br></div><div><br></div><div>and use it like this:<br></div><div> list.stream().gather(findIndex(s -> s.contains("o"))).findFirst().orElse(-1);<br></div><div><br></div><div>But it's more verbose.<br></div><div><br></div><div>I wonder if at the same time that the Gatherer API is introduced, the Collector API should be enhanced to support short-circuiting collectors ?<br></div><div><br></div><div>regards,<br></div><div>Rémi<br></div><div><br></div><div><br></div><hr id="m_3409512792581686759m_5463014457698901237m_-4157093933715390893m_9119089240644209637zwchr"><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><b>From: </b>"ІП-24 Олександр Ротань" <<a href="mailto:rotan.olexandr@gmail.com" rel="noreferrer" target="_blank">rotan.olexandr@gmail.com</a>><br><b>To: </b>"core-libs-dev" <<a href="mailto:core-libs-dev@openjdk.org" rel="noreferrer" target="_blank">core-libs-dev@openjdk.org</a>><br><b>Sent: </b>Friday, April 19, 2024 5:59:39 PM<br><b>Subject: </b>Addition of Predicate-based findIndex and findLastIndex methods to java.util.List<br></blockquote></div><div><blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;color:rgb(0,0,0);font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir="ltr">Subject<br>Addition of Predicate-based findIndex and findLastIndex methods to java.util.List<br><br>Motivation<br>The motivation behind this proposal is to enhance the functionality of the List interface by providing a more flexible way to find the index of an element. Currently, the indexOf and lastIndexOf methods only accept an object as a parameter. This limits the flexibility of these methods as they can only find the index of exact object matches.<br><br>Here I want to propose methods that would accept a Predicate as a parameter, allowing users to define a condition that the desired element must meet. This would provide a more flexible and powerful way to find the index of an element in a list.<br><br>The changes I am proposing are implemented in this PR: <a href="https://github.com/openjdk/jdk/pull/18639" rel="noreferrer" target="_blank">https://github.com/openjdk/jdk/pull/18639</a>. Here is a brief overview of the changes made in this pull request:<br><br>Added the findIndex (Predicate<? super E> filter) method to the List interface.<br>Added the findLastIndex (Predicate<? super E> filter) method to the List interface.<br>Implemented these methods in all non-abstract classes that implement the List interface, as well as List itself (default impl).<br>The changes have been thoroughly tested to ensure they work as expected and do not introduce any regressions. The test cases cover a variety of scenarios to ensure the robustness of the implementation.<br><br>For example, consider the following test case:<br><br>List<String> list = new ArrayList<>();<br>list.add("Object one");<br>list.add("NotObject two");<br>list.add("NotObject three");<br><br>int index1 = list.findIndex(s -> s.contains("ct t"));<br>System.out.println(index1); // Expected output: 1<br>int index2 = list. findLastIndex(s -> s.startsWith("NotObject"));<br>System.out.println(index2); // Expected output: 2<br>Currently, to achieve the same result, we would have to use a more verbose approach:<br><br>int index1 = IntStream.range(0, list.size())<br> .filter(i -> list.get(i).contains("ct t"))<br> .findFirst()<br> .orElse(-1);<br>System.out.println(index1); // Output: 1<br>int index2 = IntStream.range(0, list.size())<br> .filter(i -> list.get(i).startsWith("NotObject"))<br> .reduce((first, second) -> second)<br> .orElse(-1);<br>System.out.println(index2); // Output: 2<br><div>Or other approaches that require additional instructions and, therefore, can`t be used in all scopes (like passing argument to method).</div><div><br>I believe these additions would greatly enhance the functionality and flexibility of the List interface, making it more powerful and user-friendly. I look forward to your feedback and am open to making any necessary changes based on your suggestions.<br><br>The main reason I am publishing this proposal in the mailing system is to gather feedback from the Java developers community, especially about possible caveats related to backward compatibility of your projects. Would appreciate every opinion!</div><br><div>Best regards</div></div><br></blockquote></div></div></div></blockquote></div>
</blockquote></div>
</blockquote></div>