software, simply

"do" for a small speed up of your REPL feedback

A small blog post for a small tip for a small speed up on your REPL driven flow.

We have been hiring at Lifecheq, and I have been pleasantly surprised that all candidates used a “proper” REPL flow:

  1. Code in your IDE.
  2. Send top-level form to REPL.
  3. Go to 1.

The Clojure website has a good example of this workflow in the docs.

Following on the example for the docs:

(defn square 
  (* x x))
  (square 10))

if you find yourself moving your caret between the square fn definition and the square fn call, backwards and forwards, forwards and backwards, it is more efficient to:

  (defn square 
    (* x x))

  (square 10)

Wrapping both the function and the execution in a do block, means that the “Send top-form to REPL” will both redefine the function and run it, saving you a lot of arrow down and arrow up keystrokes, specially for long square functions.

You can also include in the do block multiple functions, and the tip also works if you are working inside a comment block, like:

(comment (do (defn square [x] (* x x)) (square 10) ))

To remove the do once you are finished, if you have only one function you can use “Raise”, or if you have multiple functions you will need to “Splice” and clean up the do and square calls.

And if you find yourself wanting to run a lot of square calls with different parameters, consider writing some proper tests and/or using RCF.

Did you enjoy it? or share!

Tagged in : Clojure