Skip to content

Instantly share code, notes, and snippets.

@shelajev
Last active April 21, 2020 11:24
Show Gist options
  • Save shelajev/22a7711c60028ae916c91c006b1403ae to your computer and use it in GitHub Desktop.
Save shelajev/22a7711c60028ae916c91c006b1403ae to your computer and use it in GitHub Desktop.
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