nuvio
nuvio

Reputation: 2625

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?

Upvotes: 5

Views: 1429

Answers (1)

Jeremy
Jeremy

Reputation: 22415

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

Upvotes: 18

Related Questions