~/blog

FizzBuzz in Emacs

I've been playing around with GNU Emacs for some time, but never really got myself to use it on a daily basis. To change this I decided that it's time to learn a little about Emacs Lisp and hopefully gain the ability to bend the editor to my needs. I choose the FizzBuzz Kata as a starting point and want to share what I learned along the way.

Emacs is not only an editor, it's in fact a powerful Lisp interpreter. Most of the built-in text editing functionality of Emacs is written in Emacs Lisp.

The first thing that you'll see when you start Emacs is the *scratch* buffer. In that buffer you can directly evaluate any Lisp expression. To use Emacs as a calculator for example you only have to type (+ 23 19) in that buffer, put your cursor after the closing parenthesis and press Control + j - voila there's "the answer"™.

Emacs also ships with a built in testing framework called ERT - Emacs Regression Testing, which I choose for my quest to implement FizzBuzz.

My first task was to write a failing test and learn how to execute it, so I came up with this beautiful crafted test-case.

(ert-deftest the-truth ()
  "the truth should be working"
  (should (equal t nil)))

Since I'm a lazy person, I tried only to read the necessary parts of the ERT documentation. My first attempt to run the test was executing the ert command with M-x ert RET. The command was fitting, but I received a prompt report that there were zero passed and zero failed tests.

After a little more digging I found out that I first had to evaluate the test-case. Typing the text into the *scratch* buffer doesn't tell Emacs to evaluate the expression and ERT will only run the globally defined test-cases. I had to go to the end of the test-case (after the closing parenthesis) and type C-x C-e. A second M-x ert RET gave me the familiar red "F" output.

The rest of the Kata was straight forward. My result looks like this:

(defun translate-fizz-buzz ()
  "translate a number into it's fizz-buzz value"
  (if (equal 0 (mod x 15))
      "FizzBuzz"
    (if (equal 0 (mod x 3))
        "Fizz"
      (if (equal 0 (mod x 5))
          "Buzz"
        x))))

(ert-deftest normal-number ()
  "normal numbers should not be translated"
  (should (equal 2 (translate-fizz-buzz 2))))

(ert-deftest divisible-by-three ()
  "numbers divisible by three should be replaced with Fizz"
  (should (equal "Fizz" (translate-fizz-buzz 3))))

(ert-deftest divisible-by-five ()
  "numbers divisible by five should be replaced with Buzz"
  (should (equal "Buzz" (translate-fizz-buzz 5))))

(ert-deftest divisible-by-three-and-five ()
  "numbers divisible by five and three should be replaced with FizzBuzz"
  (should (equal "FizzBuzz" (translate-fizz-buzz 15))))

If I got you interested and you also want to experiment with ELT, keep in mind that you have to evaluate every function again as soon as you make changes (at least when you use the *scratch* buffer).

This small exercises got me hooked to spent more time learning about Emacs and Lisp. I really like the concept that you have a powerful programming environment inside my editor and the possibility to bend the editor to my needs. The next weeks will show if I become a regular Emacs user. If you're interested in my Emacs configuration (which is not really advanced at the time of writing this post), have a look at my dotfiles repository.

Update: I use Emacs nowadays regularly. My configuration is based on Emacs Prelude. The interesting parts are in the personal folder of my fork.

about

Hi, I'm Jan Ahrens. In this blog you can read about my thoughts on various technical topics.

As you might have already guessed: My opinions are my own and don't necessarily represent those of my employer.

If you want to contact me you can use my PGP key. Its fingerprint is 3762 1152 E099 AB27 04E8 3FD1 B911 E6A2 2B4F 3B5F.

This blog is built with Jekyll. You can find its source code on GitHub.