<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Thank you both for your replies!</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">And thanks for the Gatherer example Viktor.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">It sounds like, from your email, that you want to see just how useful this method would be to add before we can justify adding it to Gatherers.<br></div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Fair enough. I have spent a couple of days using this custom Gatherer you made for me, and I will post a new email thread either tomorrow or the day after that shows my experience with it. My hope is that I can convince you that this function absolutely is worth adding.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Thank you for your time and help!</div><div class="gmail_default" style="font-family:monospace">David Alayachew<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jan 10, 2024 at 2:29 PM Viktor Klang <<a href="mailto:viktor.klang@oracle.com" target="_blank">viktor.klang@oracle.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>
<div dir="ltr">
<div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
>1/ Is there a reason why the integrator is not declared greedy (I'm still not sure to what exactly Greedy means in the context of gatherers) ?<br>
<br>
No, that was just a (benign) omission from typing the solution as I was typing the email
<span id="m_-1190550619796345134m_-4788448909283609857🙂">🙂</span><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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
>2/ Is there a reason to write the code in a OOP way like you have done instead of only having fields in State and specify the behaviors using lambdas (because with lambdas will you get inference)<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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Great question!</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
I chose to do that since I wanted to A) allocate the backing list on-demand, and also B) not allocate a new ArrayList for the downstream windows and retain the (cleared) old one (since its size could become needlessly large).<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 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)">
As a side-note, I tend to prototype using lambdas and then refactor as/when needed to either embed logic in the state or even migrate into a full-on implementation of the Gatherer interface (for instance if I want to override composition).<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="m_-1190550619796345134m_-4788448909283609857Signature">
<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</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
</div>
</div>
</div>
<div id="m_-1190550619796345134m_-4788448909283609857appendonsend"></div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<hr style="display:inline-block;width:98%">
<div id="m_-1190550619796345134m_-4788448909283609857divRplyFwdMsg" dir="ltr"><font style="font-size:11pt;color:rgb(0,0,0)" face="Calibri, sans-serif"><b>From:</b> Remi Forax <<a href="mailto:forax@univ-mlv.fr" target="_blank">forax@univ-mlv.fr</a>><br>
<b>Sent:</b> Wednesday, 10 January 2024 19:16<br>
<b>To:</b> Viktor Klang <<a href="mailto:viktor.klang@oracle.com" target="_blank">viktor.klang@oracle.com</a>><br>
<b>Cc:</b> David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>>; core-libs-dev <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject:</b> [External] : Re: Gatherers -- conditionalWindowFixed?</font>
<div> </div>
</div>
<div>
<div style="font-family:arial,helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<div><br>
</div>
<div><br>
</div>
<hr id="m_-1190550619796345134m_-4788448909283609857x_zwchr">
<div>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<b>From: </b>"Viktor Klang" <<a href="mailto:viktor.klang@oracle.com" target="_blank">viktor.klang@oracle.com</a>><br>
<b>To: </b>"David Alayachew" <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>>, "core-libs-dev" <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Sent: </b>Wednesday, January 10, 2024 2:33:45 PM<br>
<b>Subject: </b>Re: Gatherers -- conditionalWindowFixed?<br>
</blockquote>
</div>
<div></div>
<div>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hi David!<br>
<br>
Apologies for the late reply, there's been lots of catching up to do after the holidays.<br>
<br>
>I'm really excited for what this will enable for us.<br>
</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
I'm really glad to hear that <span id="m_-1190550619796345134m_-4788448909283609857x_🙂">\uD83D\uDE42</span><br>
<br>
>It is very much appreciated.<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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<span id="m_-1190550619796345134m_-4788448909283609857x_👍">\uD83D\uDC4D</span><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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
> Could we add one more method for a conditionalWindowFixed? We would need to pass in some Predicate<T>. If the predicate returns true, create a list (if it does not already exist) then add the element to it. If the predicate returns false while the list is
empty, then just move along to the next. Else if the predicate returns false while the list is non empty, pass the list down into the stream. So, you end up with Stream<T> -----> Stream<List<T>>.<br>
</div>
<div style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
What ends up under Gatherers.* does require careful triaging, ideally by first seeing user-created implementations being heavily used/useful, then potential candidate for Gatherers.*, and then at the end it might end up in Stream as a dedicated method.</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)">
By virtue of the Gatherers API, it seems pretty straight-forward for you to implement what you describe, and personally I'd probably call it something like
<i>windowBy</i>.</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)">
Just typing this up as I write this reply, it could look something like this:</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)">
<TR> Gatherer<TR, ?, List<TR>> windowBy(Predicate<TR> includeInCurrentWindow) {
<div> class State {</div>
<div> ArrayList<TR> window;</div>
<div><br>
</div>
<div> boolean integrate(TR element, Gatherer.Downstream<? super List<TR>> downstream) {</div>
<div> if (window != null && !includeInCurrentWindow.test(element)) {</div>
<div> var result = Collections.unmodifiableList(window);</div>
<div> window = null;</div>
<div> if (!downstream.push(result))</div>
<div> return false;</div>
<div> }</div>
<div><br>
</div>
<div> if (window == null)</div>
<div> window = new ArrayList<>();</div>
<div> </div>
<div> return window.add(element);</div>
<div> }</div>
<div><br>
</div>
<div> void finish(Gatherer.Downstream<? super List<TR>> downstream) {</div>
<div> if (window != null) {</div>
<div> var result = Collections.unmodifiableList(window);</div>
<div> window = null;</div>
<div> downstream.push(result);</div>
<div> }</div>
<div> }</div>
<div> }</div>
<div> return Gatherer.<TR, State, List<TR>>ofSequential(State::new, State::integrate, State::finish);</div>
}<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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
jshell> Stream.of("header", "value1", "value2", "header", "header", "value 3", "value 4", null).gather(windowBy(Predicate.not("header"::equals))).toList()<br>
$1 ==> [[header, value1, value2], [header], [header, value 3, value 4, null]]<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 style="font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
</div>
</div>
</blockquote>
<div><br>
</div>
<div>Hello,<br>
</div>
<div>I've two questions,<br>
</div>
<div>1/ Is there a reason why the integrator is not declared greedy (I'm still not sure to what exactly Greedy means in the context of gatherers) ?<br>
</div>
<div>2/ Is there a reason to write the code in a OOP way like you have done instead of only having fields in State and specify the behaviors using lambdas (because with lambdas will you get inference)<br>
</div>
<div><br>
</div>
<div><T> Gatherer<T, ?, List<T>> windowBy(Predicate<? super T> includeInCurrentWindow) {<br>
</div>
<div>
<div> class State {</div>
<div> ArrayList<T> window;</div>
<div> }<br>
</div>
<div> return Gatherer.ofSequential(</div>
<div> State::new,<br>
</div>
<div> (state, element, downstream) -> {<br>
</div>
<div> ...<br>
</div>
<div> },<br>
</div>
<div> (state, downstream) -> {<br>
</div>
<div> ...<br>
</div>
<div> });<br>
</div>
<div>} <br>
</div>
</div>
<div><br>
</div>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<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="m_-1190550619796345134m_-4788448909283609857x_Signature">
<div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif;font-size:12pt;color:rgb(0,0,0)">
Cheers,<br>
√</div>
</div>
</div>
</div>
</blockquote>
<div><br>
</div>
<div>regards,<br>
</div>
<div>Rémi<br>
</div>
<div><br>
</div>
<blockquote style="border-left:2px solid rgb(16,16,255);margin-left:5px;padding-left:5px;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;color:rgb(0,0,0)">
<div>
<div id="m_-1190550619796345134m_-4788448909283609857x_Signature">
<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>
<hr style="display:inline-block;width:98%">
<div id="m_-1190550619796345134m_-4788448909283609857x_divRplyFwdMsg" dir="ltr"><font style="font-size:11pt;color:rgb(0,0,0)" face="Calibri, sans-serif"><b>From:</b> core-libs-dev <<a href="mailto:core-libs-dev-retn@openjdk.org" target="_blank">core-libs-dev-retn@openjdk.org</a>> on behalf of David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>><br>
<b>Sent:</b> Wednesday, 10 January 2024 07:05<br>
<b>To:</b> <a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a> <<a href="mailto:core-libs-dev@openjdk.org" target="_blank">core-libs-dev@openjdk.org</a>><br>
<b>Subject:</b> Gatherers -- conditionalWindowFixed?</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div style="font-family:monospace">Hello Core Libs Dev Team,<br>
<br>
I have been reading through JEP 461 (<a href="https://openjdk.org/jeps/461" id="m_-1190550619796345134m_-4788448909283609857OWA4aedc79b-5bff-f738-0eb6-be35009f56cf" target="_blank">https://openjdk.org/jeps/461</a>) about Gatherers, and I'm really
excited for what this will enable for us.<br>
<br>
By far, the most important functionality that this API facilitates is the ability to create windows. Anytime I needed a window, I was basically forced to use a for loop. Now, the 2 most common window cases are being handed to us for free. It is very much appreciated.<br>
<br>
Could we add one more method for a conditionalWindowFixed? We would need to pass in some Predicate<T>. If the predicate returns true, create a list (if it does not already exist) then add the element to it. If the predicate returns false while the list is empty,
then just move along to the next. Else if the predicate returns false while the list is non empty, pass the list down into the stream. So, you end up with Stream<T> -----> Stream<List<T>>.<br>
<br>
The reason I think this is worth adding is because it facilitates a really common use case. We may not want all windows to be the same size.<br>
<br>
Is this something worth adding to the Gatherers API?<br>
<br>
Thank you for your time and help!<br>
David Alayachew<br>
</div>
</div>
</div>
<br>
</blockquote>
</div>
</div>
</div>
</div>
</div></blockquote></div>