<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<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!<br>
<br>
I've been thinking a bit about it [the problem] and arrived somewhere along the lines of it not necessarily being worth adding a new type to represent an index and an element (or trying to repurpose something existing), as I think the ergonomics of providing
 a mapper BiFunction is better, somewhere along the lines of:<br>
<br>
</div>
<div class="elementToProof" style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
public static <T, R> Gatherer<T, ?, R> mapIndexed(BiFunction<Long, ? super T, ? extends R> mapper) {</div>
<div class="elementToProof" style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
   class Index { long at = 0; }</div>
<div style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
   return Gatherer.ofSequential(</div>
<div style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
       Index::new,</div>
<div style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
       (idx, e, d) -> d.push(mapper.apply(idx.at++, e))</div>
<div style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
   );</div>
<div style="line-height: 18px; white-space: pre; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
}</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);">
(Using longs for indexing seems sensible for something which could have unbounded length)<br>
<br>
Which would mean that if you have your own Pair class, or want to represent it as a Map.Entry, it's pretty straight-forward to do:<br>
<br>
stream.gather(mapIndexed(Pair::of))...</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);">
It is unfortunate that parallelization takes a hit in this use-case, but knowing what indicies a sub-segment of the Stream has depends on the known size of the stream¡Xand I wouldn't be surprised that out-of-order processing of indices can be surprising to people,
 so perhaps an ofSequential(¡K) isn't all that bad.<br>
<br>
With that being said, including new Gatherers in the stdlib is important to be done only after thorough evaluation of need.</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 id="Signature" class="elementToProof" style="color: inherit;">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,<br>
¡Ô</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b><br>
</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<b>Viktor Klang</b></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Software Architect, Java Platform Group<br>
Oracle</div>
</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 Olexandr Rotan <rotanolexandr842@gmail.com><br>
<b>Sent:</b> Thursday, 5 December 2024 18:20<br>
<b>To:</b> Henrik Wall <xehpuk.dev@gmail.com><br>
<b>Cc:</b> core-libs-dev <core-libs-dev@openjdk.org><br>
<b>Subject:</b> Re: JEP 473: Proposal for new built-in gatherer `indexed`</font>
<div> </div>
</div>
<div>
<div dir="auto">
<p dir="ltr">Hi. There has been a proposal from me (that Chen mentioned), approximately half a year ago. At the time I have insisted on creating stream sub interface, and even got a working prototype for sequential streams, but there have been such a huge complexity
 blowup in parallisation that I have just decided to drop it. Gatherers can be used pretty easily for this task, but using ofSequential, sacrificing parallelism. So basically, parallelism is a pain point here (or performance). I am not saying that it is impossible
 to console enumeration and prallelisation, but it will require huge efforts and invasive changes in current *Pipeline implementations, or enormous amounts of code duplication</p>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Thu, Dec 5, 2024, 18:48 Henrik Wall <<a href="mailto:xehpuk.dev@gmail.com" target="_blank" rel="noreferrer">xehpuk.dev@gmail.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0 0 0 .8ex; border-left:1px #ccc solid; padding-left:1ex">
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>
</blockquote>
</div>
</div>
</body>
</html>