Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a python lambda to either scala or java. Is this possible using py4j?

I currently am creating a python api for an apache spark framework and would like to use the underlying java or scala code I already have written to drive most of my python. The issue is that I need python lambda expressions that can be converted to java or scala so that they can be re-used within my existing framework. Is there any way to do this using py4j?

like image 587
Ghastone Avatar asked Aug 12 '26 20:08

Ghastone


1 Answers

Py4J supports implementing Java interfaces from Python - see here.

So you need to create Python classes that implement the interfaces in java.util.function. For example:

class Function:
    def __init__(self, gateway, lambda_function):
        self.gateway = gateway
        self.lambda_function = lambda_function

    def apply(self, arg):
        return self.lambda_function(arg)

    class Java:
        implements = ["java.util.function.Function"]

As an example of how to use it - suppose you have a Java method that takes a Function<Integer, String>:

public class PythonLambdasExample {  
    public static String callFunction(Function<Integer, String> function) {
        return function.apply(42);
    }
}

You can invoke it from Python like this:

>>> l = lambda i : "The number was %d" % i
>>> function = Function(gateway, l)
>>> result = gateway.jvm.PythonLambdasExample.callFunction(function)
>>> print(result)
The number was 42
like image 87
migwellian Avatar answered Aug 15 '26 10:08

migwellian