Last active
April 21, 2020 11:24
-
-
Save shelajev/22a7711c60028ae916c91c006b1403ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Arrays; | |
import org.graalvm.polyglot.*; | |
public class MyClass { | |
public static void main(String[] args) { | |
try (Context context = Context.newBuilder().allowAllAccess(true).build()) { | |
context.eval("python", | |
"import polyglot\n" + | |
"@polyglot.export_value\n" + | |
"def doubleMe(x):\n" + | |
" return [a * 2 for a in x]"); | |
// explore v.as(Function.class) to convert to java.util.Function | |
Value doubleMe = context.getBindings("python").getMember("doubleMe"); | |
List input = Arrays.asList(1, 2, 3); | |
Value result = doubleMe.execute(input); | |
List resultAsJavaObject = result.as(List.class); | |
System.out.println(resultAsJavaObject); // prints [2, 4, 6] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment