How to print to STDERR in Clojure?

There is no specific function for this, however you can dynamically rebind the var holding the stream that println writes to like this: (println “Hello, STDOUT!”) (binding [*out* *err*] (println “Hello, STDERR!”)) In my REPL, the colour indicates the stream (red is STDERR):

Are there any Clojure DSLs?

Like any Lisp dialect, Clojure draws a very fuzzy line between API and DSL and therefore the term doesn’t hold the same mystique that it does in other languages. Lisp programmers tend to write their programs as layers of DSLs, each layer serving those above it. Having said that, here are a few that you … Read more

Idiomatic clojure map lookup by keyword

(:color my-car) is fairly standard. There are a few reasons for this, and I won’t go into all of them. But here’s an example. Because :color is a constant, and my-car is not, hotspot can completely inline the dynamic dispatch of color.invoke(m), which it can’t do with m.invoke(color) (in some java pseudo-code). That gets even … Read more

Clojure: binding vs. with-redefs

Clojure Vars can have thread-local bindings. binding uses these, while with-redefs actually alters the root binding (which is someting like the default value) of the var. Another difference is that binding only works for :dynamic vars while with-redefs works for all vars. Examples: user=> (def ^:dynamic *a* 1) #’user/*a* user=> (binding [*a* 2] *a*) 2 … Read more

How do multimethods solve the namespace issue?

Dynamic dispatch and namespace resolution are two different things. In many object systems classes are also used for namespaces. Also note that often both the class and the namespace are tied to a file. So these object systems conflate at least three things: class definitions with their slots and methods the namespace for identifiers the … Read more

Why does concat on vectors evaluate to a list?

concat returns a lazy sequence. user=> (doc concat) ————————- clojure.core/concat ([] [x] [x y] [x y & zs]) Returns a lazy seq representing the concatenation of the elements in the supplied colls. you can convert it back to a vector with into: user=> (into [] (concat [1 2] [3 4] [5 6])) [1 2 3 … Read more

tech