Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of Processors in Clojure? Java interop

I'd like to use the Java method: Runtime.getRuntime().availableProcessors() and save the result in a integer variable.

So in Clojure I did this:

(def n-cpu  ((.availableProcessors (Runtime/getRuntime)) ))

and this:

(def n-cpu (Integer/parseInt ((.availableProcessors (Runtime/getRuntime)) )))

but none work.

Any suggestions?

like image 747
nuvio Avatar asked Apr 03 '12 18:04

nuvio


1 Answers

If you replace the method call in your version with an integer, this is what you logically have:

(def n-cpu (4))

Clojure cannot handle the list (4) because the first item in a non-quoted list must be a function. In this case the first item is an integer, and Clojure doesn't treat integers as functions. If you remove the unnecessary parentheses, your var definition would look like this:

(def n-cpu (.availableProcessors (Runtime/getRuntime)))

Notice how if you replace the method call with an integer, it becomes (def n-cpu 4)?

like image 99
Jeremy Avatar answered Oct 03 '22 06:10

Jeremy