Re: how to use a static method as a stream Function

Jeff Allen <[email protected]> Sun, 31 Dec 2023 15:54:43 +0000
Newsgroups gmane.comp.lang.jython.devel
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============1511816698713418625==
Content-Type: multipart/alternative;
 boundary="------------senJry2THL2d3eCb0fp0PdKN"
Content-Language: en-GB

This is a multi-part message in MIME format.
--------------senJry2THL2d3eCb0fp0PdKN
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

I managed to do this (in 2.7.3):

 >>> from java.lang import Double
 >>> from java.util import Arrays
 >>> from java.util.function import DoubleBinaryOperator
 >>> class DoubleSum(DoubleBinaryOperator):
...     applyAsDouble = Double.sum
...
 >>> doubleSum = DoubleSum()
 >>> dd = list(map(float, range(10)))
 >>> dd
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
 >>> Arrays.stream(dd).reduce(-3.0, doubleSum)
42.0

I chose Double here for a quick test, rather than Float, because I 
wasn't sure the coercion to float would work. There does not seem to be 
a way to supply Double.sum directly but you can wrap it as shown in the 
right type with the right method name. This seems to me not far from 
what the Java compiler is doing for you when you use ::.

I haven't stepped through to see what happens under the covers. Is this 
better than the delegation you indicate?

Jeff

On 30/12/2023 22:26, Albert Cardona wrote:
> Hi all,
>
> Is there any way to use something akin to the double colon notation,
> to use a static method as a Function in a collection stream?
>
> Otherwise, if I have to instantiate it as a jython class that then
> delegates to the static method, performance suffers greatly.
>
> For example, how can one use java.lang.Float's sum method, which takes
> two floating-point numbers as arguments, in a reduce operation?
>
> Thank you.
>
> Best wishes,
>
> Albert
>
>
> _______________________________________________
> Jython-dev mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jython-dev

-- 

Jeff Allen

--------------senJry2THL2d3eCb0fp0PdKN
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>I managed to do this (in 2.7.3):</p>
    <p><font face="monospace">&gt;&gt;&gt; from java.lang import Double<br>
        &gt;&gt;&gt; from java.util import Arrays<br>
        &gt;&gt;&gt; from java.util.function import DoubleBinaryOperator<br>
        &gt;&gt;&gt; class DoubleSum(DoubleBinaryOperator):<br>
        ...     applyAsDouble = Double.sum<br>
        ...<br>
        &gt;&gt;&gt; doubleSum = DoubleSum()<br>
        &gt;&gt;&gt; dd = list(map(float, range(10)))<br>
        &gt;&gt;&gt; dd<br>
        [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]<br>
        &gt;&gt;&gt; Arrays.stream(dd).reduce(-3.0, doubleSum)<br>
        42.0</font></p>
    <p>I chose Double here for a quick test, rather than Float, because
      I wasn't sure the coercion to float would work. There does not
      seem to be a way to supply Double.sum directly but you can wrap it
      as shown in the right type with the right method name. This seems
      to me not far from what the Java compiler is doing for you when
      you use ::.</p>
    <p>I haven't stepped through to see what happens under the covers.
      Is this better than the delegation you indicate?<br>
    </p>
    <p>Jeff<br>
    </p>
    <div class="moz-cite-prefix">On 30/12/2023 22:26, Albert Cardona
      wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAJj13__hVVpk-cQQb7HnoWy1iDiJ2c_BWYFOb_c-L_MMC25-Yw@mail.gmail.com">
      <pre class="moz-quote-pre" wrap="">Hi all,

Is there any way to use something akin to the double colon notation,
to use a static method as a Function in a collection stream?

Otherwise, if I have to instantiate it as a jython class that then
delegates to the static method, performance suffers greatly.

For example, how can one use java.lang.Float's sum method, which takes
two floating-point numbers as arguments, in a reduce operation?

Thank you.

Best wishes,

Albert


_______________________________________________
Jython-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="https://lists.sourceforge.net/lists/listinfo/jython-dev">https://lists.sourceforge.net/lists/listinfo/jython-dev</a>
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 

Jeff Allen
</pre>
  </body>
</html>

--------------senJry2THL2d3eCb0fp0PdKN--


--===============1511816698713418625==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============1511816698713418625==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Jython-dev mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-dev

--===============1511816698713418625==--