-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert Python function to Java Function #17
Comments
I know this is a bit of a different terrain, but this issue reminds me of scijava/scijava-common#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:
A similar Then, each Python function (or 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:
😎
The text was updated successfully, but these errors were encountered: