Endless Parentheses

Concise ramblings on Emacs productivity.

Be a 4clojure hero with Emacs

This year I made it my resolution to learn clojure. After reading through the unexpectedly engaging romance that is Clojure for the Brave and True, it was time to boldly venture through the dungeons of 4clojure. Sword in hand, I install 4clojure.el and start hacking, but I felt the interface could use some improvements.

For starters, it's annoying that you need two commands to check the answer and move to next question. Sacha has a nice suggestion on this matter, a single command which checks the answer and moves to the next question. Nonetheless, I needed more.

Having to manually erase the __ fields to type my answer is absurd. Not to mention some answers are cumbersome to type inline. With the following command, you type your code at the end of the buffer; a lone archer lining up a shot to slay the problems above.

(defun endless/4clojure-check-and-proceed ()
  "Check the answer and show the next question if it worked."
  (interactive)
  (unless
      (save-excursion
        ;; Find last sexp (the answer).
        (goto-char (point-max))
        (forward-sexp -1)
        ;; Check the answer.
        (cl-letf ((answer
                   (buffer-substring (point) (point-max)))
                  ;; Preserve buffer contents, in case you failed.
                  ((buffer-string)))
          (goto-char (point-min))
          (while (search-forward "__" nil t)
            (replace-match answer))
          (string-match "failed." (4clojure-check-answers))))
    (4clojure-next-question)))

The second encounter is simple in comparison. Just sharpen the blade, polish the shield, and we're ready for battle.

(defadvice 4clojure/start-new-problem
    (after endless/4clojure/start-new-problem-advice () activate)
  ;; Prettify the 4clojure buffer.
  (goto-char (point-min))
  (forward-line 2)
  (forward-char 3)
  (fill-paragraph)
  ;; Position point for the answer
  (goto-char (point-max))
  (insert "\n\n\n")
  (forward-char -1)
  ;; Define our key.
  (local-set-key (kbd "M-j") #'endless/4clojure-check-and-proceed))

These two snippets have me cleaving effortlessly through the initial questions and I'm eager for the final challenges.

Both of these websites were recommended to me by Michael Fogleman. Do you know any other good clojure resources?

Tags: clojure, programming, init.el, emacs

Say thanks on Gratipay
comments powered by Disqus