<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div dir="ltr"><meta http-equiv="content-type" content="text/html; charset=utf-8"><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="lineBreakAtBeginningOfMessage"><div class="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="Apple-interchange-newline"><div><meta charset="UTF-8"><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">Greetings,</div><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><br></div><div class="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; -webkit-text-stroke-width: 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="Apple-converted-space"> </span><b>Stream Gatherers</b><span class="Apple-converted-space"> </span>is now a<span class="Apple-converted-space"> </span><a href="https://openjdk.org/jeps/461" title="https://openjdk.org/jeps/461" id="LPlnk995248" class="OWAAutoLink">Preview JEP with Candidate status</a></div><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><br></div><div class="elementToProof 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; -webkit-text-stroke-width: 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="Apple-converted-space"> </span><a href="https://github.com/viktorklang-ora/jdk/tree/gatherer" title="https://github.com/viktorklang-ora/jdk/tree/gatherer" id="OWA8ab01dac-cb46-bda0-009f-d3a4a0a6e81e" class="OWAAutoLink ContentPasted0">here</a>.<br></div><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><br></div><div class="elementToProof 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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">While<span class="Apple-converted-space"> </span><a href="https://cr.openjdk.org/~vklang/Gatherers.html" title="https://cr.openjdk.org/~vklang/Gatherers.html " id="LPlnk332869" class="OWAAutoLink">the design</a><span class="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="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><br></div><div class="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; -webkit-text-stroke-width: 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="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; -webkit-text-stroke-width: 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="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="Apple-converted-space"> </span></b>was renamed to<span class="Apple-converted-space"> </span><b>Gatherer::initializer()</b><span class="Apple-converted-space"> </span>to better reflect intent.<br><br></li><li style="list-style-type: disc;"><b>Gatherer.Sink<R></b><span class="Apple-converted-space"> </span>was renamed to<span class="Apple-converted-space"> </span><b>Gatherer.Downstream<R></b><span class="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="Apple-converted-space"> </span>and its companion type<span class="Apple-converted-space"> </span><b>Gatherer.ThenCollector</b><span class="Apple-converted-space"> </span>was dropped due to compatibility concerns with existing code which operates on<span class="Apple-converted-space"> </span><b>Collector</b>.<br><br></li><li style="list-style-type: disc;"><div><span><b>Gatherer.Characteristics</b><span class="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="Apple-converted-space"> </span><b>Gatherer.defaultCombiner()</b>, and<span class="Apple-converted-space"> </span><b>Gatherer.defaultFinisher()</b><span class="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="Apple-converted-space"> </span><b>Gatherer::combiner()</b>, and<span class="Apple-converted-space"> </span><b>Gatherer::finisher()</b><span class="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="Apple-converted-space"> </span><b>Integrator</b><span class="Apple-converted-space"> </span>named<span class="Apple-converted-space"> </span><b>Greedy</b><span class="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="Apple-converted-space"> </span><b>Integrator</b><span class="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="Apple-converted-space"> </span><b>Gatherer.of()</b><span class="Apple-converted-space"> </span>and<span class="Apple-converted-space"> </span><b>Gatherer.ofSequential()</b><span class="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="Apple-converted-space"> </span><b>java.util.stream.Gatherers</b><br></span></li></ul></div><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"></div><div class="elementToProof 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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">I recently presented<span class="Apple-converted-space"> </span><a href="https://www.youtube.com/watch?v=8fMFa6OqlY8" title="https://www.youtube.com/watch?v=8fMFa6OqlY8" id="LPlnkOWALinkPreview_2" class="OWAAutoLink">Gatherers at Devoxx</a>, which I'd recommend watching for an introduction to the feature.<br></div><div class="_Entity _EType_OWALinkPreview _EId_OWALinkPreview_8 _EReadonly_1" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><div id="LPBorder_BVTaHR0cHM6Ly93d3cueW91dHViZS5jb20vd2F0Y2g:dj04Zk1GYTZPcWxZOA.." class="LPBorder874039" style="width: 777px; margin-top: 16px; margin-bottom: 16px; position: relative; 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="position: relative; margin-right: 12px; height: 180px; overflow: hidden; width: 240px;"><a target="_blank" id="LPImageAnchor874039" href="https://www.youtube.com/watch?v=8fMFa6OqlY8"><span class="AppleTemporaryEditingElement" id="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://www.youtube.com/watch?v=8fMFa6OqlY8" 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="http://www.youtube.com/">www.youtube.com</a></div></td></tr></tbody></table></div></div><div class="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; -webkit-text-stroke-width: 0px; text-decoration: none; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><span> </span><br></div><div class="elementToProof" style="caret-color: rgb(0, 0, 0); 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; -webkit-text-stroke-width: 0px; text-decoration: none;"><div style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><br></div><div id="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></body></html>