Chicken-NBT: A Minecraft NBT reader for Chicken Scheme

I didn’t see any Scheme code to read Minecraft NBT data files, so I made one. And in doing so, I see why there weren’t any NBT readers in Scheme. (Haskell programmers can use Adam Foltzer’s much cleaner Haskell version.)

When I found out that there was a gzip egg called z3, I thought my troubles were over. Not quite, because although z3 made it ridiculously easy to read compressed data, the only real procedure I had to read data from the gzip stream was read-byte. And not only did I have to read only one byte at a time, but they were all promoted to ints anyways, since there is no notion of different-width integers in Chicken (only fixnums).

Continue reading Chicken-NBT: A Minecraft NBT reader for Chicken Scheme

Writing reusable JavaScript

One of the biggest hurdles in going from JavaScript as a trivial gimmick for making divs change color to a powerful language for building real applications is its single-namespace weakness. It’s easy to accidentally cause variable names to collide when you start combining scripts, and there’s no native library or module system to alleviate this. For example, jQuery, prototype.js, and MooTools all want to use “$” as a shortcut variable, which has caused me problems before when combining software written by other people.

It’s easy to write a bunch of JavaScript that drops variables all over the global namespace, but often, you want to reuse some JavaScript component in multiple pages. Let’s look at a hypothetical example:
Continue reading Writing reusable JavaScript

HTML templating in Scheme?

Templating is one of those must-haves for building websites; separating content from presentation is sliced bread compared to cavalierly interleave logic and HTML into one file. In Scheme, I think that quasiquotation could be used in wonderful ways to create templates; namely, every template can be a function that simply takes parameters and directly uses quasiquotation as the templater:

(define blog-post-template
  (lambda (title date message comments)
    `(html
       (head (title "My Blog - " ,title))
       (body
         (div (@ (class "post"))
           (h1 ,title)
           ,message)
         (div (@ (class "comments"))
           (p ,comments) ...)))))

and then to use it, you would just use

(blog-post-template
  "Another pedantic blog post"
  "Nov 20, 2010"
  "Today I'm going to complain about everything ..."
  '("Awesome blog!"
    "Actually, I disagree with you on everything"
    "Spam"))

You could also add a little more flexibility into your template by accepting an association list of parameters rather than fixed parameters:

(define blog-post-template
  (lambda params
    `(html
       (head (title "My Blog - " ,(cadr (assq 'title params))))
       (body))))

which can be made easier to use by redefining quasiquote with some templating-specific conveniences.

Petite Chez Scheme for Ubuntu

An Ubuntu package of Petite Chez Scheme seems to be a common request among students, given the popularity of Ubuntu among the techno-capable. ¬†Currently, only RPM packages are provided on the Chez Scheme site, and the traditional recommendation was to use alien to convert the RPM into a .deb. There’s no reason to have everybody do this though.

I’ve built a .deb package of Petite Chez Scheme for amd64 (64-bit) Ubuntu 10.10. You should be able to install it by simply double-clicking the package and following the on-screen prompts. Petite can then be run using the terminal command `petite`.

Known issues: No package docs.

Reading a USB Stamps.com scale

Update: I now have an improved C version of this program: usbscale.

I got suckered into one of those hard-to-cancel Stamps.com trials. The upside is that they give you a $10 USB 5 lb. scale to use with their software. The downside is that they want you to only use it with their software, and the company that makes the scale has since taken down their free USB-scale program.

The good news, as Nicholas Piasecki and some Linux users figured out, is that the USB scale conforms to the USB HID specifications, which helpfully standardize how USB scales should work (no joke).

So, this little Perl hack reads from the Stamps.com scale by accessing the hidraw# interface that Linux provides. In my case, I have hidraw4 hard-coded into the script itself. Basically, it loops until it reads a good value from the scale, at which point it prints out the weight and exits.

Edit: This code is now a Gist on GitHub.

Continue reading Reading a USB Stamps.com scale

Google Summer of Code 2009: WordPress proposal

Objective: Create a single-file PHP installer for WordPress that will automate the downloading, unpacking, and setup of a WordPress blog.

Reason: As of right now, setting up a WordPress blog involves a lot of fiddling with files. The user must download the archive, unzip it on their computer, open a ftp connection to their server, upload all of the contents, hope that all the permissions are right, and navigate to the right directory. Since WordPress is such a popular blogging software, many people (most of whom are not familiar with a Unix shell or do not have access to one) would benefit from a single-file installer.

Deliverable: One (1) PHP file. No other files can be included with this (unless embedded), since this would void much of the advantage of such a single-file installer.

Basic procedure (a rough plan):

  1. Instruct the user on the requirements of WordPress (needs hosting with PHP, etc.).
  2. Instruct the user on downloading and uploading this installer, and subsequently running it.
  3. Check the server environment for required components, correct permissions, etc.
  4. Give the user some options on name, location, etc. and solicit other options like database credentials.
  5. Download the latest WordPress distribution.
  6. Unpack the distribution to the right place.
  7. Proceed more-or-less with the “usual” install.

Constructor Chaining in Java

When you have a class that extends another class in Java, initializing that child class will first call the parent’s initializer, and then the child’s. So if you have:

public class Base {
    public Base() {
        System.out.print("Base  ");
    }
}
public class Derived extends Base {
    public Derived() {
        System.out.print("Derived");
    }
}

The code Derived d1 = new Derived(); will print Base Derived.