🥷 Clojure Pro Tip 4: Cider ClojureDocs

Sometimes, we may want a few examples of a Clojure API for inspiration while developing, especially when reading other people's or open source code.

With Cider in Emacs, we can run M-x cider-doc to see the docstring for the symbol. We can even use M-x cider-clojuredocs for some examples if the symbol is from the language core.

Running M-x cider-clojuredocs
Running M-x cider-clojuredocs

I happened to find this command recently. I bet I was not the only one who didn't know it, thus I'm sharing.

[Read More]

Getting Started with Cider for Clojure Programming

Here is the outline for my cider tutorial on YouTube, covering basic things you need to know to get started with cider, and starting exploring the fun of clojure programming with the REPL-driven programming approach.

Jack In to a REPL

C-c M-j (cider-jack-in-clj)
start a nREPL and jack in. It works in a project or with a sole .clj file.
M-x cider-connect-clj
run the command and then fill in hostname and port. It could be useful in some cases. e.g. on Windows, I can start the nREPL manually and then connect to it separately.

Evaluate Things

C-M-x (cider-eval-defun-at-point)
evaluate current top-level form.
C-x C-e (cider-eval-last-sexp)
evaluate the preceding form.
C-c C-k (cider-load-buffer)
evaluate/load the current buffer.
C-c C-p (cider-pprint-eval-last-sexp)
Pprint the result in a dedicated buffer. Great thing to do when the result is too large to fit in the echo area.

Note: I prefer to use the same key bindings as for Elisp, given that cider might bind a few keys to a single command!

[Read More]

A Few Quick Notes About babashka/fs

Recently I've used babashka/fs a little bit, here are some quick notes for it:

  • Path vs File. Use Path whenever possible, according to this SO answer to "Java: Path vs File". This is actually Java related.
  • It's ok to use a path as a key for a clojure map. At my first try, I somehow came to the conclusion that it's not ok, while I was refactoring the live reload for clay. And later I found that it's totally fine to use it as a key. 😞
  • Use fs/path to construct file/directory paths, for example, (fs/path "/tmp" "foo" "bar" "baz.clj") will result in a path object for /tmp/foo/bar/baz.clj on linux. If you're coming from Python, it might remind you of os.path.join(path, *paths).
  • Use fs/createdirs for mkdir -p, though its name is a bit misleading, I first thought it's for creating a few dirs.
  • fs/with-temp-dir is convenient for creating tests.

Doing Unit test in Clojure Is Easy

While refactoring the live reload feature of Clay, I realized I'd better break long functions into smaller and functional ones (as many as I can), which is also a common practice in the clojure community.

Small pure functions not only are easy to verify on the development process (using a REPL), but also are easy to test. And unit tests are easy to write in clojure, just use deftest from clojure.test, a very basic one looks like this:

[Read More]

Starting a Clojure nREPL Manually for Cider

While troubleshooting and fixing a live reload bug in Clay today, which required to start a minimal Clojure environment, I figured out how to start a nREPL from the command line.

Instead of using M-x cider-jack-in-clj directly from Emacs, actually we can manully bring up an nREPL with this: clj -Sdeps "{:deps {org.scicloj/clay {:mvn/version \"2-beta21\"} cider/cider-nrepl {:mvn/version \"0.50.2\"}}}" -m nrepl.cmdline --middleware "[cider.nrepl/cider-middleware]" (The clay part is only necessary for this debugging), and then connect to this nREPL using the Emacs command M-x cider-connect-clj.

[Read More]