Calling Clojure from Java
From rzezeski on the clojure mailing list:------ CallClojure.java ------ import clojure.lang.RT; import clojure.lang.Var; import clojure.lang.PersistentVector; public class CallClojure { static PersistentVector toVec(int[][] arr) { PersistentVector pv = PersistentVector.EMPTY; for (int[] a : arr) { PersistentVector temp = PersistentVector.EMPTY; for (int n : a) { temp = temp.cons(n); } pv = pv.cons(temp); } return pv; } public static void main(String[] args) throws Exception { RT.loadResourceScript("foo.clj"); Var gimmie_vec = RT.var("foo", "gimmie-vec"); int[][] ar = {{1, 2}, {3, 4}, {5, 6}}; Object result = gimmie_vec.invoke(toVec(ar)); System.out.println(result); } } ------ foo.clj ------ (ns foo) (defn gimmie-vec [v] (println "class:" (class v)) (println "v:" v))
0 comments:
Post a Comment