<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Henrik,</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I recall there was a thread for EnumeratedStream (<a href="https://mail.openjdk.org/pipermail/core-libs-dev/2024-April/thread.html#121871">https://mail.openjdk.org/pipermail/core-libs-dev/2024-April/thread.html#121871</a>) back in April; it calls for the same
feature, to add a stage for indexing entries.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
This approach of using a gatherer is simple; its disadvantage is that when there is a next stage in the stream, the indices become invalidated and cause confusions for users. This is not a problem in the Rust/Python enumerate examples, as they do not intend
to be fluent like Stream.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I had a toy project that explores the consumption of (index, element) pairs: <a href="https://github.com/liachmodded/IndexedStream/blob/main/src/main/java/com/github/liachmodded/indexedstream/IndexedGatherers.java">
https://github.com/liachmodded/IndexedStream/blob/main/src/main/java/com/github/liachmodded/indexedstream/IndexedGatherers.java</a></div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
This simple example takes predicates and functions that take an (index, element) pair and return a boolean/a next element. In the same package, there is also an Indexed.java API, which allows you to create Collector/Gatherer for an indexed entry, and re-fit
that Collector/Gatherer to be general purpose.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
These are all aimed to avoid propagating no-longer-valid indices to the next stage of the stream operations; and they aren't exactly pretty.</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Do you have any justifications for the addition of this particular gatherer, so it should be provided in the Java Class Libraries instead of a user library?</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Regards,</div>
<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Chen Liang</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> core-libs-dev <core-libs-dev-retn@openjdk.org> on behalf of Henrik Wall <xehpuk.dev@gmail.com><br>
<b>Sent:</b> Thursday, December 5, 2024 10:48 AM<br>
<b>To:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> JEP 473: Proposal for new built-in gatherer `indexed`</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hey folks,<br>
<br>
Not having access to the index of an element of a stream is often a<br>
reason to fall back to a traditional loop, at least for me. I'd love<br>
to have `Gatherers.indexed()` that looks something like this:<br>
<br>
public static <TR> Gatherer<TR, ?, Map.Entry<Integer, TR>> indexed() {<br>
return Gatherer.ofSequential(<br>
() -> new int[1],<br>
Gatherer.Integrator.ofGreedy((state, element, downstream) -><br>
downstream.push(Map.entry(state[0]++, element)))<br>
);<br>
}<br>
<br>
(Potentially with a custom pair class to avoid auto-boxing.)<br>
<br>
In other popular languages like Python or Rust, this is also called `enumerate`.<br>
<br>
Any chance to get that in a future release?<br>
<br>
Regards,<br>
Henrik<br>
</div>
</span></font></div>
</body>
</html>