Calling Clojure from Java

by 6/16/2010 07:11:00 PM 0 comments

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))

Brian Schlining

Developer

Cras justo odio, dapibus ac facilisis in, egestas eget quam. Curabitur blandit tempus porttitor. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.

0 comments: