<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Hi Tyler,</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
Thank you for the kind words -- they are much appreciated. And you have a very good question indeed!<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
My thinking behind making <i>fold</i> a Gatherer is that I think that it is strictly more powerful than "only" having it as a collector (It wouldn't be able to be a Collector since you'd need a combiner for it, but also being able to compose it with other operations,
 choosing the output type at a later stage in the process, and so forth).<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Also, if you think about it -- single-element Streams are just as valid as empty Streams, N-sized Streams, or even unbounded Streams, and conceptually, there's no difference between the following two Streams:</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
var a = Stream.of("1234")<br>
var b = Stream.of(1,2,3,4).gather(fold(() -> "", (str, next) -> str + next))</div>
<div class="elementToProof">
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
So that was my thinking—allow developers to stay within Stream processing for as long as they want, and choose the terminal operation when they need to break out from the Stream.</div>
<div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div 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">
<div>
<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<br>
</div>
</div>
</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> Tyler Kindy <me@tylerkindy.com><br>
<b>Sent:</b> Monday, 30 October 2023 12:14<br>
<b>To:</b> Viktor Klang <viktor.klang@oracle.com><br>
<b>Cc:</b> core-libs-dev@openjdk.org <core-libs-dev@openjdk.org><br>
<b>Subject:</b> [External] : Re: Update on JEP-461: Stream Gatherers (Preview)</font>
<div> </div>
</div>
<div dir="auto">
<div dir="ltr">
<div dir="ltr">Thanks for the JEP and your talk, Viktor! I think `Stream::gather` will be super useful in my day-to-day as a Java developer.
<div><br>
</div>
<div>I’m curious why `<font face=".AppleSystemUIFont">fold`</font> is being implemented with gatherers. I recognize `Gatherer` is designed to support intermediate operations, but `fold` feels inherently like a terminal operation to me since it, like `reduce`
 or `collect`, consumes all the elements in the stream and produces a single result.</div>
<div><br>
</div>
<div>Is there a technical limitation to making `fold` a terminal operation? For example, does `Collector` inherently presume parallelization in a way that `Gatherer` does not?</div>
<div><br>
</div>
<div>Or is the idea mainly to demonstrate the power of gatherers, and we could also make `fold` a terminal operation with the current `Stream` API?</div>
<div><br>
</div>
<div>Thank you!</div>
<div>Tyler Kindy<br id="x_lineBreakAtBeginningOfMessage">
<div class="x_AppleOriginalContents" style="direction:ltr"><br>
<blockquote type="cite">
<div>On Oct 27, 2023, at 9:50 AM, Viktor Klang <viktor.klang@oracle.com> wrote:</div>
<br class="x_Apple-interchange-newline">
<div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
Greetings,</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
As you may have already seen,<span class="x_Apple-converted-space"> </span><b>Stream Gatherers</b><span class="x_Apple-converted-space"> </span>is now a<span class="x_Apple-converted-space"> </span><a href="https://openjdk.org/jeps/461" title="https://openjdk.org/jeps/461" id="LPlnk995248" class="x_OWAAutoLink">Preview
 JEP with Candidate status</a></div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div class="x_elementToProof x_ContentPasted0" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
Work-in-progress (interfaces, implementation, tests, benches, and documentation) for JEP-461 is currently available<span class="x_Apple-converted-space"> </span><a href="https://urldefense.com/v3/__https://github.com/viktorklang-ora/jdk/tree/gatherer__;!!ACWV5N9M2RV99hQ!Lz2sC02xW35XpuwoaqBvD_iR80Xrzkbj-60oOKuQklUOm8e69-O3WC9N93leBFbMkFmULJmsat9k1dmt$" title="https://github.com/viktorklang-ora/jdk/tree/gatherer" id="OWA8ab01dac-cb46-bda0-009f-d3a4a0a6e81e" class="x_OWAAutoLink x_ContentPasted0">here</a>.<br>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div class="x_elementToProof x_ContentPasted2" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
While<span class="x_Apple-converted-space"> </span><a href="https://cr.openjdk.org/~vklang/Gatherers.html" title="https://cr.openjdk.org/~vklang/Gatherers.html " id="LPlnk332869" class="x_OWAAutoLink">the design</a><span class="x_Apple-converted-space"> </span>has
 held up well, there are some important improvements made since the original design document was written.<br>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
Notable changes (without any particular order):</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<ul data-editing-info="{"orderedStyleType":1,"unorderedStyleType":1}">
<li style="list-style-type:disc"><b>Stream::gather()</b><span class="x_Apple-converted-space"> </span>now has a default implementation.<br>
<br>
<span></span></li><li style="list-style-type:disc"><b><span>Gatherer</span><span></span>::supplier()<span class="x_Apple-converted-space"> </span></b>was renamed to<span class="x_Apple-converted-space"> </span><b>Gatherer::initializer()</b><span class="x_Apple-converted-space"> </span>to
 better reflect intent.<br>
<br>
</li><li style="list-style-type:disc"><b>Gatherer.Sink<R></b><span class="x_Apple-converted-space"> </span>was renamed to<span class="x_Apple-converted-space"> </span><b>Gatherer.Downstream<R></b><span class="x_Apple-converted-space"> </span>to better signal what
 it represents.<br>
<br>
</li><li style="list-style-type:disc"><b>Gatherer::collect(Collector)</b><span class="x_Apple-converted-space"> </span>and its companion type<span class="x_Apple-converted-space"> </span><b>Gatherer.ThenCollector</b><span class="x_Apple-converted-space"> </span>was
 dropped due to compatibility concerns with existing code which operates on<span class="x_Apple-converted-space"> </span><b>Collector</b>.<br>
<br>
</li><li style="list-style-type:disc">
<div><span><b>Gatherer.Characteristics</b><span class="x_Apple-converted-space"> </span>have been eliminated and superseded by having default values that are used as sentinels.<br>
</span></div>
 (discussed further down the list)<br>
<div><span>This is important because with the Characteristics-model keeping alignment between Characteristics and actual implementation proved brittle, and under composition of Gatherers computing the union was inefficient and mostly lead to an empty set in
 the end.<br>
<br>
</span></div>
</li><li style="list-style-type:disc"><span><b>Gatherer.defaultInitializer()</b>,<span class="x_Apple-converted-space"> </span><b>Gatherer.defaultCombiner()</b>, and<span class="x_Apple-converted-space"> </span><b>Gatherer.defaultFinisher()</b><span class="x_Apple-converted-space"> </span>were
 added as static methods—these are the sentinels used to elide calling the initializer, to elide calling the combiner (avoid parallelization), and to elide calling the finisher, respectively.<br>
<br>
</span></li><li style="list-style-type:disc"><span><b>Gatherer::initializer()</b>,<span class="x_Apple-converted-space"> </span><b>Gatherer::combiner()</b>, and<span class="x_Apple-converted-space"> </span><b>Gatherer::finisher()</b><span class="x_Apple-converted-space"> </span>default
 implementations return the respective sentinels.<br>
<br>
</span></li><li style="list-style-type:disc"><span>A subtype of<span class="x_Apple-converted-space"> </span><b>Integrator</b><span class="x_Apple-converted-space"> </span>named<span class="x_Apple-converted-space"> </span><b>Greedy</b><span class="x_Apple-converted-space"> </span>was
 added, together with a factory method to guide lambda-to-nominal-type conversion. This allows creators of Gatherers to signal that an<span class="x_Apple-converted-space"> </span><b>Integrator</b><span class="x_Apple-converted-space"> </span>will never initiate
 a short-circuit (but may relay one from downstream), and that is available during evaluation to determine if the operation can short-circuit or not.</span>
<div style="list-style-type:disc"><br>
<span></span></div>
</li><li style="list-style-type:disc"><span>Factories for creating anonymous Gatherers were expanded upon to include<span class="x_Apple-converted-space"> </span><b>Gatherer.of()</b><span class="x_Apple-converted-space"> </span>and<span class="x_Apple-converted-space"> </span><b>Gatherer.ofSequential()</b><span class="x_Apple-converted-space"> </span>with
 different sets of parameters, primarily to make it more ergonomical and easier to read and write the code using those factories.</span>
<div style="list-style-type:disc"><br>
<span></span></div>
</li><li style="list-style-type:disc"><span>A curated set of initial built-in Gatherers is located in<span class="x_Apple-converted-space"> </span><b>java.util.stream.Gatherers</b><br>
</span></li></ul>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
</div>
<div class="x_elementToProof x_ContentPasted1" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
I recently presented<span class="x_Apple-converted-space"> </span><a href="https://urldefense.com/v3/__https://www.youtube.com/watch?v=8fMFa6OqlY8__;!!ACWV5N9M2RV99hQ!Lz2sC02xW35XpuwoaqBvD_iR80Xrzkbj-60oOKuQklUOm8e69-O3WC9N93leBFbMkFmULJmsam2MP-_S$" title="https://www.youtube.com/watch?v=8fMFa6OqlY8" id="LPlnkOWALinkPreview_2" class="x_OWAAutoLink">Gatherers
 at Devoxx</a>, which I'd recommend watching for an introduction to the feature.<br>
</div>
<div class="x__Entity x__EType_OWALinkPreview x__EId_OWALinkPreview_8 x__EReadonly_1" style="font-family:Helvetica; font-size:12px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<div id="LPBorder_BVTaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g:dj04Zk1GYTZPcWxZOA.." class="x_LPBorder874039" style="width:777px; margin-top:16px; margin-bottom:16px; max-width:800px; min-width:424px">
<table id="LPContainer874039" role="presentation" style="padding:12px 36px 12px 12px; width:777px; border-width:1px; border-style:solid; border-color:rgb(200,200,200); border-radius:2px">
<tbody>
<tr valign="top" style="border-spacing:0px">
<td>
<div id="LPImageContainer874039" style="margin-right:12px; height:180px; overflow:hidden; width:240px">
<a target="_blank" id="LPImageAnchor874039" href="https://urldefense.com/v3/__https://www.youtube.com/watch?v=8fMFa6OqlY8__;!!ACWV5N9M2RV99hQ!Lz2sC02xW35XpuwoaqBvD_iR80Xrzkbj-60oOKuQklUOm8e69-O3WC9N93leBFbMkFmULJmsam2MP-_S$"><span class="x_AppleTemporaryEditingElement" id="x_cid:d048f613-b144-4ff9-a2d3-98e0765aab57"><Outlook-cztoycvq.jpg></span></a></div>
</td>
<td style="width:465px">
<div id="LPTitle874039" style="font-size:21px; font-weight:300; margin-right:8px; font-family:wf_segoe-ui_light,"Segoe UI Light","Segoe WP Light","Segoe UI","Segoe WP",Tahoma,Arial,sans-serif; margin-bottom:12px">
<a target="_blank" id="LPUrlAnchor874039" href="https://urldefense.com/v3/__https://www.youtube.com/watch?v=8fMFa6OqlY8__;!!ACWV5N9M2RV99hQ!Lz2sC02xW35XpuwoaqBvD_iR80Xrzkbj-60oOKuQklUOm8e69-O3WC9N93leBFbMkFmULJmsam2MP-_S$" style="text-decoration:none">Teaching
 old Streams new tricks By Viktor Klang</a></div>
<div id="LPDescription874039" style="font-size:14px; max-height:100px; font-family:wf_segoe-ui_normal,"Segoe UI","Segoe WP",Tahoma,Arial,sans-serif; margin-bottom:12px; margin-right:8px; overflow:hidden; color:rgb(102,102,102)">
Have you ever wanted to perform an operation on a java.util.stream.Stream only to find that the existing set of operations didn't provide what you needed—forcing you to break out early from the Stream and perform the logic outside of it? As a matter of fact,
 java.util.stream was the first JDK API designed with lambdas in mind and was ...</div>
<div id="LPMetadata874039" style="font-size:14px; font-weight:400; font-family:wf_segoe-ui_normal,"Segoe UI","Segoe WP",Tahoma,Arial,sans-serif; color:rgb(166,166,166)">
<a href="https://urldefense.com/v3/__http://www.youtube.com/__;!!ACWV5N9M2RV99hQ!Lz2sC02xW35XpuwoaqBvD_iR80Xrzkbj-60oOKuQklUOm8e69-O3WC9N93leBFbMkFmULJmsalMQI96j$">www.youtube.com</a></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="x_elementToProof" style="font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none; font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<span>      </span><br>
</div>
<div class="x_elementToProof" style="font-family:Helvetica; font-size:12px; font-style:normal; font-variant-caps:normal; font-weight:400; letter-spacing:normal; text-align:start; text-indent:0px; text-transform:none; white-space:normal; word-spacing:0px; text-decoration:none">
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif; font-size:12pt">
<br>
</div>
<div id="x_Signature">
<div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">Cheers,<br>
√</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt"><br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt"><b><br>
</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt"><b>Viktor Klang</b></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">Software Architect, Java Platform Group<br>
Oracle</div>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<br>
</div>
</div>
</div>
</div>
</body>
</html>