Offensive Code

While writing code to format Roman Numerals, I was made aware of the Rosetta Stone collection of code snippets to do the same.  I read the code, and after looking at it for a few minutes, I found myself oddly offended.

By the code.

Here it is:

(defun ar2ro (AN)
  "translate from arabic number AN to roman number,
   ar2ro(1666) returns (M D C L X V I)"
  (cond
   ((>= AN 1000) (cons 'M (ar2ro (- AN 1000))))
   ((>= AN 900) (cons 'C (cons 'M (ar2ro (- AN 900)))))
   ((>= AN 500) (cons 'D (ar2ro (- AN 500))))
   ((>= AN 400) (cons 'C (cons 'D (ar2ro (- AN 400)))))
   ((>= AN 100) (cons 'C (ar2ro (- AN 100))))
   ((>= AN 90) (cons 'X (cons 'C (ar2ro (- AN 90)))))
   ((>= AN 50) (cons 'L (ar2ro (- AN 50))))
   ((>= AN 40) (cons 'X (cons 'L (ar2ro (- AN 40)))))
   ((>= AN 10) (cons 'X (ar2ro (- AN 10))))
   ((>= AN 5) (cons 'V (ar2ro (- AN 5))))
   ((>= AN 4) (cons 'I (cons 'V (ar2ro (- AN 4)))))
   ((>= AN 1) (cons 'I (ar2ro (- AN 1))))
   ((= AN 0) nil)))

First of all, I really don’t like the repetition of the constants (1000 … 1000, 900 … 900).

Second of all, I don’t like that the subtractive elements (CM for 900) are in there explicitly, since they can be easily derived.  You may argue that it’s defensible in this case, since there aren’t really that many possible Roman “digits”, but writing repetitious code like that is error prone.  (And virtually all the Rosetta Stone examples, in all the languages, do the subtractive elements explicitly.)

Which leads me to the third point, which is that the algorithm is wrong.  9 is encoded as VIV, since whoever typed this forgot to write the “9” line.

The offensive part is, I think, that this is code that no humans should write.  It’s so pattern-based that one should write code that generates that code.  But in that case, you might as well just write the algorithm “properly”, which leads to smaller code, anyway:

(defvar roman-numeral-mapping
  '((1000 . "M") (500 . "D") (100 . "C") (50 . "L")
    (10 . "X") (5 . "V") (1 . "I")))

(defun format-roman-numeral (number)
  "Format NUMBER into a Roman numeral.
Roman numerals look like \"XCVII\"."
  (let ((mapping roman-numeral-mapping)
         values roman elem subtract)
    ;; Add the subtractive elements ("IV", etc) to the mapping.
    (while (setq elem (pop mapping))
      (push elem values)
      ;; We use the feature that the subtractive element is alternatively
      ;; one or two elements further along in the list.
      (when (setq subtract (elt mapping (mod (1+ (length mapping)) 2)))
        (push (cons (- (car elem) (car subtract))
                    (concat (cdr subtract) (cdr elem)))
              values)))
    ;; Compute the Roman numeral.
    (dolist (value (nreverse values))
      (while (>= number (car value))
        (push (cdr value) roman)
        (setq number (- number (car value)))))
    (apply 'concat (nreverse roman))))

Remove the comments, and it’s fewer lines.

(Now, this might be characterised as “smarty-pants programming”.  The normal way to have a variable that flips between 0/1 would be to, well, have a variable that flips between 0/1.  Instead I’m using the decreasing length of a list (modulo 2) to get the same effect.  It’s slower, and it makes the code more obscure, but, hey, it saves a couple of lines.  Other than that, I don’t think I’ve cheated much.)

I don’t often see code that I think is downright offensive.  I mostly see good code, or bad code, or WTF code, but seldom code that squicks me out.  But the Rosetta code did.

Posting to WordPress from Emacs

I’ve grown increasingly annoyed (I annoy easily) with the HTML editor at WordPress.com. Not that the HTML editor at Blogger was any better. Editing blog entries is just fiddly when you have images and stuff.

And you have to upload the images. Boooring.

WordPress has a “post by email” feature, so I wondered what would happen if I sent a multipart/related HTML message, with images embedded in the email. The documentation didn’t say anything about this, so I thought the likelihood of them being nerdy enough to handle that was slim.

But look:

That’s the view from the hotel room in London… And this?

That’s Salt Lake City.

See? It works!

Now, Emacs didn’t support sending multipart/related messages, so I had to implement that first. In Ma Gnus now, if you compose an HTML message, and include <img> things that point to local files, then Message/mml will now rewrite the HTML to have internal cid: links, and append the images to the email. So you just send out huge emails, WordPress parses it, and this is the result.

Behold! The first resulting blog message!

Now to see whether I can control all relevant bits about how the images are placed and stuff…

My New Concert Blog

Multipletap at Cafe Oto was a lovely two-day festival featuring a butt-load of great Japanese peeps. Mostly noise stuff, but Ko Ishikawa played a lovely flute set, and Yumiko Tanaka did a very intense string-based thing.

Day one was good, and on day two they fixed everything that didn’t work on day one. Seating, videos, sound problems, etc. Iteration!

New Gmane SSDs

The Gmane news spool is 97% full, so I either had to delete some Gwene stuff, or buy more SSDs. Image

I bought more SSDs.  The current setup is 5x 512GB Samsungs in RAID5.  I bought 5x 1TB while in the US, so that gives us 2x the current size in RAID5, which should be enough for the next uhm five years? or so?

But the problem is how to do the switchover.  The last time it was pretty seamless.  I set up a new, spiffy machine with a spiffy hardware RAID controller. (See my secret diary for details.) Then synced all the articles over, and swapped some IP addresses at the end.

I really don’t want to buy another spiffy RAID controller this time.  But if I’m reusing the current hardware, there’s going to be downtime.

Here’s the plan:

1) Sync the spool over to an external SATA disk.

2) Take the server down, swap in the new SSDs, set up the RAID.

3) Rsync the articles from the external SATA disk to the RAID.

5) Profit!

2-5 will realistically take a day or so, with Gmane being totally dead while this is happening.  I think.

Hm…  or I could point the spool to the external disk while doing 3.  In read only mode.  Then there should only be a few hours downtime while I’m doing the RAID rebuild.  Hm.  Yes, I think that sounds doable…

So a few hours complete deadness, and a day or so in read-only mode.

But it’s a bit scary doing it this way.  If I were doing a completely separate new server, there would always be an easy way to roll back if things don’t work…