<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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;">
<span style="color: rgb(0, 0, 0);">Hi R¨¦mi,<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().</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>
<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> 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$">
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$">
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>
</body>
</html>