Multiline Clojure docstrings

If you’re using Emacs, grab clojure-mode.el from technomancy’s Github, which differs from the one in ELPA (I don’t know why, both claim to be version 1.11.5, maybe someone can comment on that?) but includes clojure-fill-docstring which will format docstrings with nice indentation and linewrapping, bound by default to C-c M-q. It will take this: (defn … Read more

Reason for skipping AOT?

This isn’t specific to noir but one scenario you might want to skip AOT for a given namespace is when deploying your code to a PaaS provider such as heroku. Heroku performs AOT compilation of your code by default so consider this snippet in your server.clj: (db/connect! (System/getenv “DB_URL”)) (defn start [port] (run-jetty app {:port … Read more

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

tech