Fast Prime Number Generation in Clojure

Here’s another approach that celebrates Clojure’s Java interop. This takes 374ms on a 2.4 Ghz Core 2 Duo (running single-threaded). I let the efficient Miller-Rabin implementation in Java’s BigInteger#isProbablePrime deal with the primality check. (def certainty 5) (defn prime? [n] (.isProbablePrime (BigInteger/valueOf n) certainty)) (concat [2] (take 10001 (filter prime? (take-nth 2 (range 1 Integer/MAX_VALUE))))) … Read more

What is Clojure volatile?

The new volatile is as close as a real “variable” (as it is from many other programming languages) as it gets for clojure. From the announcement: there are a new set of functions (volatile!, vswap!, vreset!, volatile?) to create and use volatile “boxes” to hold state in stateful transducers. Volatiles are faster than atoms but … Read more

Can I clean the repl?

If you want to clear the current namespace of all temporary variables and functions you declared you can use this one liner (or make a function of it) : (map #(ns-unmap *ns* %) (keys (ns-interns *ns*))) or (ns myutil) (defn ns-clean “Remove all internal mappings from a given name space or the current one if … Read more

Where to put specs for Clojure.Spec?

I usually put specs in their own namespace, alongside the namespace that they are describing. It doesn’t particularly matter what they’re named, as long as they use some consistent naming convention. For example, if my code is in my.app.foo, I’ll put specs in my.app.foo.specs. It is preferable for spec key names to be in the … Read more