-
Notifications
You must be signed in to change notification settings - Fork 2
Java Interoperability
Sebastian Hoß edited this page Jul 11, 2013
·
1 revision
To call one of the existing functions from Java code, use the following pattern:
Var require = RT.var("clojure.core", "require"); Symbol namespace = Symbol.intern("DESIRED.NAMESPACE.HERE"); Var function = RT.var("DESIRED.NAMESPACE.HERE", "DESIRED-FUNCTION"); Keyword keyword = Keyword.intern("REQUIRED-KEYWORD");require.invoke(namespace); Object result = function.invoke(keyword, VALUE);
The following example uses (ratio/debt-ratio) to calculate the debt ratio:
Var require = RT.var("clojure.core", "require"); Symbol ratio = Symbol.intern("com.github.sebhoss.finj.ratio"); Var debtRatio = RT.var("com.github.sebhoss.finj.ratio", "debt-ratio"); Keyword totalDebt = Keyword.intern("total-debt"); Keyword totalAssets = Keyword.intern("total-assets");require.invoke(ratio); Object result = debtRatio.invoke(totalDebt, 100, totalAssets, 1000); System.out.println(result); // => 1/10