<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"Viktor Klang" <viktor.klang@oracle.com><br><b>To: </b>"Remi Forax" <forax@univ-mlv.fr>, "core-libs-dev" <core-libs-dev@openjdk.java.net><br><b>Sent: </b>Thursday, January 2, 2025 12:26:08 PM<br><b>Subject: </b>Re: [External] : Gatherers.fold()<br></blockquote></div><div><style style="display:none;"> P {margin-top:0;margin-bottom:0;} </style></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">


<div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;">
<span style="color: rgb(0, 0, 0);">Hi Rémi,</span></div></blockquote><div><br></div><div>Happy new year,<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div class="elementToProof" style="font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; font-size: 12pt;"><span style="color: rgb(0, 0, 0);"><br>
>Thinking a little more, i do not understand why fold takes a Supplier as first parameter and not just a value given that the Gatherer is created with ofSequential().</span></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);">
If it didn't take a supplier then the resulting Gatherer could never be reused (including composition). I.e. The use of Supplier for the state type is not primarily/only about parallelizability, but rather reuse (which subsumes parallelizability).</div></blockquote><div><br></div><div>yes, it makes sense.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>Okay, so there is just a missing wildcard when the Supplier is declared in fold, thanks to the erasure (it's not something you often write :) ), it's a backward compatible change.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;">
<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);">
</div></div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>Rémi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><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);"><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>

<hr style="display:inline-block;width:98%">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Remi Forax <forax@univ-mlv.fr><br><b>Sent:</b> Friday, 20 December 2024 10:39<br><b>To:</b> core-libs-dev <core-libs-dev@openjdk.java.net><br><b>Cc:</b> Viktor Klang <viktor.klang@oracle.com><br><b>Subject:</b> [External] : Gatherers.fold()</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;"><div class="PlainText">Hello Victor,<br>
for the Advent of code of yesterday [1], i've this code golfing solution (the full code is here [2])<br>
<br>
  var input = ...;<br>
<br>
  class Node {<br>
    final HashMap<Integer, Node> map = new HashMap<>();<br>
    boolean end;<br>
  }<br>
  var root = new Node();<br>
  Arrays.stream(input.substring(0, input.indexOf('\n')).split(", "))<br>
    .forEach(w -> w.chars().boxed().gather(Gatherers.fold(() -> root,<br>
          (n, c) -> n.map.computeIfAbsent(c, _ -> new Node()))).findFirst().orElseThrow().end = true);<br>
  println(input.lines().skip(2).filter(w -><br>
      w.chars().boxed().<Set<Node>>gather(Gatherers.fold(() -> new HashSet<>(Set.of(root)),<br>
        (ms, c) -> ms.stream().flatMap(m -> Stream.ofNullable(m.map.get(c)))<br>
          .flatMap(n -> n != null && n.end ? Stream.of(n, root) : Stream.ofNullable(n)).collect(Collectors.toSet())))<br>
        .findFirst().orElseThrow().stream().anyMatch(n -> n.end)).count());<br>
<br>
<br>
As you can see, the second call to Gatherers.fold() requires to specify the type argument, the <Set<Node>> in front because the supplier is not declared as a Supplier<? extends ...><br>
<br>
Thinking a little more, i do not understand why fold takes a Supplier as first parameter and not just a value given that the Gatherer is created with ofSequential().<br>
<br>
regards,<br>
Rémi<br>
<br>
[1] <a href="https://urldefense.com/v3/__https://adventofcode.com/2024/day/19__;!!ACWV5N9M2RV99hQ!J6APK3GQDbT-T1edAyWssnGHideZ-3rnwBQ39CsaHnc0y6OLh0fpSub1gPWjXsfzHl4OfAtxfQvA1k3dUfis$" target="_blank">
https://urldefense.com/v3/__https://adventofcode.com/2024/day/19__;!!ACWV5N9M2RV99hQ!J6APK3GQDbT-T1edAyWssnGHideZ-3rnwBQ39CsaHnc0y6OLh0fpSub1gPWjXsfzHl4OfAtxfQvA1k3dUfis$</a>
<br>
[2] <a href="https://urldefense.com/v3/__https://github.com/forax/advent-of-code-2024__;!!ACWV5N9M2RV99hQ!J6APK3GQDbT-T1edAyWssnGHideZ-3rnwBQ39CsaHnc0y6OLh0fpSub1gPWjXsfzHl4OfAtxfQvA1n-w8kEG$" target="_blank">
https://urldefense.com/v3/__https://github.com/forax/advent-of-code-2024__;!!ACWV5N9M2RV99hQ!J6APK3GQDbT-T1edAyWssnGHideZ-3rnwBQ39CsaHnc0y6OLh0fpSub1gPWjXsfzHl4OfAtxfQvA1n-w8kEG$</a>
<br>
<br>
<br>
</div></span></font></div><br></blockquote></div></div></body></html>