How to pass list(collection) as a parameter to a clojure function, this clojure is called by java code.
Asked
Active
Viewed 2,790 times
2 Answers
1
Clojure:
(ns utils ; Sets the namespace to utils
(:gen-class :name Utils ; The keyword :gen-class declares that
; I want this compiled as a class. The
; :name declares the name (and the package)
; of the class I want created.
:methods [#^{:static true} [sum [java.util.Collection] long]]))
; In the vector following :methods I've declared
; the methods I want to have available in the
; generated class. So I want the function 'sum'
; which takes a 'java.util.Collection' as an
; argument and returns a value of type 'long'.
; The metadata declaration '#^{:static true}
; signals that I want this method to be declared
; static.
; The Clojure function. Takes a collection and
; sums the values in the collection using 'reduce'
; and '+'.
(defn sum [coll] (reduce + coll))
; The wrapper function that is available to Java.
; Just calls 'sum'.
(defn -sum [coll] (sum coll))
Java:
public class CalculateSum {
public static void main(String[] args) {
java.util.List<Integer> xs = new java.util.ArrayList<Integer>();
xs.add(10);
xs.add(5);
System.out.println(Utils.sum(xs));
}
}
This prints out 15.

ponzao
- 20,684
- 3
- 41
- 58
-
ponzao, tell me the way to compile and run this code on window xp. javac -cp clojure.jar CalculateSum.java, java -cp clojure.jar CalclulateSum........will work? – vikbehal Oct 05 '11 at 11:03
-
D:\work\test-Clojure>javac -cp clojure.jar CalculateSum.java CalculateSum.java:6: cannot find symbol symbol : variable Utils location: class CalculateSum System.out.println(Utils.sum(xs)); ^ 1 error D:\work\test-Clojure> – vikbehal Oct 05 '11 at 11:09
-
@vikbehal You can compile CalculateSum without the Clojure dependency, so `javac CalculateSum.java` is enought. Then you have to compile the Clojure source file. For instance in the repl with `(compile 'utils)`. Then just run it with the classpath properly set `java -cp .:../lib/clojure-1.2.1.jar CalculateSum` (this is on Linux). – ponzao Oct 05 '11 at 11:10
-
-
thanx man, this is working fine now. I have one more question! link is here [link] (http://stackoverflow.com/questions/7659423/is-it-possible-to-pass-a-tree-data-structure-to-clojure-and-work-on-it) – vikbehal Oct 05 '11 at 11:35
-
@vikbehal I added comments, but I recommend you read the link given in mikera's post. I think it does a better job of explaining this than me. – ponzao Oct 05 '11 at 13:49
-
@vikbehal If there isn't a question here already about creating a JAR file on Windows I recommend creating one. The answer to this is clearly out of the scope of your original question. – ponzao Oct 10 '11 at 11:02
0
You might want to look at the great answers to the question on calling clojure from Java
There's nothing special particularly about a list: any Java object can be passed as a parameter to a Clojure function in the same way.
-
thankyou mikera, i tried to pass a list as a parameter but wasn't successful. if possible could you please give me a small sample program. (clj and java)? – vikbehal Oct 05 '11 at 10:59