From: Jeff A. <ja...@fa...> - 2023-12-31 16:19:49
|
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 > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-dev -- Jeff Allen |