Open ctrueden opened 4 years ago
I know this is a bit of a different terrain, but this issue reminds me of https://github.com/scijava/scijava-common/issues/295.
Here is a JPype version of the above example code:
import jpype
jpype.startJVM()
@jpype.JImplements('java.util.function.Function')
class PythonFunction:
def __init__(self, function):
self.function = function
@jpype.JOverride
def apply(self, o):
return self.function(o)
def fib(n):
if n == 0 or n == 1:
return 1;
return fib(n - 1) + fib(n - 2)
jfib = PythonFunction(fib)
ArrayList = jpype.JClass('java.util.ArrayList')
jlist = ArrayList()
jlist.addAll([5, 4, 3, 2, 1])
jvals = jlist.stream().map(jfib).toArray()
print(f"type(jvals) = {type(jvals)}")
print(f"type(jvals[0]) = {type(jvals[0])}")
print(f"jvals = {jvals}")
produces:
type(jvals) = <java class 'java.lang.Object[]'>
type(jvals[0]) = <java class 'java.lang.Long'>
jvals = [8, 5, 3, 2, 1]
A similar JImplements
proxy could be made for all of the various functional interfaces that are part of the Java standard library (Supplier
, BiFunction
, Consumer
, BiConsumer
, etc.).
Then, each Python function (or Callable
more generally perhaps) could be wrapped to the an appropriate interface by examining its signature. But just because we could do that, doesn't mean we should... I don't have a good use case for automagical inference in this way, and it might not end up being doable—e.g. the only way I know of to glean whether a Python function returns anything is via type hints, which might not be present.
I think we should wait to think about this any further for the moment... I foresee returning to this issue as the SciJava Ops project gets further along and we start wanting to write existing Python functions as ops...
It would be nice to support something like this:
Where
FunctionFromPython
is:Then anything operating on a Java
Function
can accept a Pythonfunction
with appropriate conversion. For example:Prints:
😎