<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Didn't Brian or one of the Amber folks say that we might (some time in the near future) get a .match() method on streams, which can take in a pattern? It's the pattern version of .map().</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Apr 26, 2025 at 3:13 PM Chen Liang <<a href="mailto:chen.l.liang@oracle.com" target="_blank">chen.l.liang@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 style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Hi Nir, I think currently the most similar code pattern in stream that enjoys a good performance is:</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
.<MyClass>mapMulti((x, sink) -> {</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
if (x instanceof MyClass myClass) {</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
sink.accept(myClass);</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
}</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
})</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
>From the language point of view, such pattern-match-and-map-0/1 operations may be common.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
That said, can we provide a language feature in the future to convert a pattern declaration to BiConsumer<T, XxxConsumer<args>>? I believe that is a better way to address the question you have raised here.</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Regards,</div>
<div style="font-family:"Calibri Light","Helvetica Light",sans-serif;font-size:12pt;color:rgb(0,0,0)">
Chen Liang</div>
<div id="m_7444840946401389239m_1587004422300216797appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_7444840946401389239m_1587004422300216797divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><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 Nir Lisker <<a href="mailto:nlisker@gmail.com" target="_blank">nlisker@gmail.com</a>><br>
<b>Sent:</b> Saturday, April 26, 2025 1:55 PM<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> Casting gatherer</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi,
<div><br>
</div>
<div>instanceof has been refitted to include an auto-cast ("pattern matching on instanceof"). Unfortunately, doing this as an intermediate operation on a stream requires first to filter via instanceof and then map via a cast. This is because</div>
<div><br>
</div>
<div> x instanceof MyClass myClass</div>
<div><br>
</div>
<div>returns a boolean, not myClass.</div>
<div><br>
</div>
<div>I've asked for an easier way of doing it long ago directly on Stream and was declined, but now with Gatherers I'm bringing this up again. I think it would be reasonable to put such an operation in the Gatherers class. I imagine that many Gatherer libraries,
or utility classes, will include it since it's a common operation, and having it in the JDK means that it'll be done the best way possible (you can optimize where others can't, if applicable here).</div>
<div><br>
</div>
<div>-- Nir</div>
</div>
</div>
</div>
</div></blockquote></div>