Namespaces in Clojure

Despite several good texts that attempted to explain them (including the outstanding Clojure for the Brave and True), I had until recently a fuzzy understanding of how namespaces (object naming in general, really) work in Clojure. I finally bit the bullet and dug into the surprisingly readable source code for Clojure and sorted things out for myself.

To start at the global scope and drill down: the Clojure runtime maintains a map from symbols to namespaces. I don’t see a way to expose this directly as a map within Clojure itself, but all-ns will iterate over the namespaces. You can then use ns-name to get the symbolic name for a namespace if you need it.

The current namespace is determined by the value of the dynamic Var clojure.core/*ns*.

As it turns out, namespaces are surprisingly simple. A namespace is just a pair of maps: one from symbols to Vars and classes (the namespace map, returned by ns-map), and one from symbols to namespaces (the alias map, returned by ns-aliases). The namespace map is what refers to the objects contained in the namespace; e.g., if you enter foo/bar (or just bar if foo is your current namespace) in your code or at the REPL, the evaluator looks in foo‘s namespace map for a Var or class associated with the symbol bar. The alias map provides aliases for one or more namespaces within another namespace. E.g., if namespace foo has an entry in its alias map from the symbol b to the namespace baz and foo is the current namespace, b/bar will be equivalent to baz/bar.

One detail that I hadn’t been clear on before: Vars contain a reference to the namespace in which they are interned, as well as the symbol with which they are initially associated. Namespaces themselves do not directly track which Vars are interned in them; if you call ns-interns, it just filters the Var values in the namespace map to only include those that claim the namespace in question as the one in which they are interned. The symbol that the Var gets when it is created is as far as I can tell only used for documentation, not identification. If foo is interned into namespace ns1, referred into namespace ns2 as bar, and then removed from ns1 using ns-unmap, the Var returned will still call itself #'ns1/foo, even though it is only accessible as ns2/bar.

Ejabberd 2.1.13 with GSSAPI/Kerberos authentication for all Ubuntu distributions now available

Owing to popular demand (i.e., a fellow from Germany asked nicely), my ejabberd-gssapi PPA now has GSSAPI/Kerberos-enabled ejabberd packages for all currently-supported Ubuntu distributions except Lucid; i.e.:

  • Ubuntu 12.04—Precise Pangolin
  • Ubuntu 12.10—Quantal Quetzal
  • Ubuntu 13.04—Raring Ringtail
  • Ubuntu 13.10—Saucy Salamander

Initial thoughts on Scala

I just finished writing my first program in Scala, a simple Black-Scholes binomial-tree derivative pricer.

So far, the most notable observation I can make about the language is HOLY CRAP THAT’S A LOT OF SYNTACTIC SUGAR.

Got a method? You can call it using “object.method()”, like in C++/Java/C#. If it takes zero or one parameters, you can also call it as “object method” or “object method parameter“, so you can use it as an operator. (Left-associative, unless the method name ends with a colon, then it’s right-associative.)

Do you have a function that only takes one parameter? You can call it with curly braces instead of parentheses, which lets you use the loan pattern.

In a situation where you need to provide a function where you’ll only need to reference each parameter once, and in the order provided? Just use underscores in the function body for the parameters rather than having to declare them.

Calling a parameterless function? You can leave off the parentheses. Probably.

There’s a lot of stuff like this. The good thing about all this is that it lets Scala have syntax for features that resemble those in other languages, and a lot of that is in libraries, not the base language specification. Lists can be created and pattern-matched using the cons operator like Lisp. Case classes look a lot like Haskell, and are similarly suited for pattern-matching. You can create actors and send them messages using “!” like in Erlang. You can magically grab the most relevant thing in some contexts with “_” like in Perl.

Traits are a substantial improvement over single inheritance + interfaces (à la Java/C#) or multiple inheritance (à la C++).

For a language that’s all about strong typing, its inference system has some surprising weak spots. Type erasure prevents you from being able to match on a parameterization of a generic class, and I already got caught by a failure to infer types when dealing with zipped lists.

For all that kvetching, though, it’s got most of the nice features that I’d expect from both object-oriented and functional languages: first-class functions, immutable data types, concise syntax for map/reduction/folding, classes, lazy evaluation, singleton objects, generics, etc.

Serial console to the rescue

Interesting recent discovery: at least some modern consumer-grade routers have serial consoles, albeit inconvenient to get to. I bricked a new WNR3500Lv2 by flashing a Tomato image that was meant for the v1 onto it. I wasn’t pleased by the thought that I had destroyed a $60 piece of hardware, but after a bit of Googling I found this post that mentioned the possibility of starting the TFTP server to flash a new image from the serial console. One USB/TTL cable purchase later, I was back in business.

Laptop connected to WNR3500Lv2 via USB/TTL cable

Ejabberd 2.1.13 with GSSAPI/Kerberos authentication for Ubuntu 12.04 LTS (Precise Pangolin) available

I’m currently working with the maintainers of the Debian ejabberd package to add GSSAPI/Kerberos support. As it will be surely be a while before the new packages find their way into the Debian repository, and even longer until they find their way into Ubuntu, I’ve created a PPA containing packages for the current version of ejabberd (2.1.13 at the time of writing) with the GSSAPI/Kerberos patch and the esasl module on which the GSSAPI patch relies.

The PPA can be added to your system manually by copying the lines below and adding them to your system’s software sources.

deb http://ppa.launchpad.net/ngroot/ejabberd-gssapi/ubuntu precise main 
deb-src http://ppa.launchpad.net/ngroot/ejabberd-gssapi/ubuntu precise main 

If you would find packages for another distribtion of Ubuntu to be useful, please comment and I will try to accommodate your request.

GSSAPI/Kerberos authentication in PIdgin

I’ve been working on creating an ejabberd package with mikma/badlop’s XMPP GSSAPI support patch applied. I wanted to use Pidgin as a client to test it. Pidgin claims to support Kerberos authentication, but I was unable to find documentation on how to set it up.

After digging through the source of the Jabber authentication module in libpurple, I figured out what was going on: there is no explicit “use Kerberos” option in the settings for an account. If a server lists GSSAPI as one of its supported mechanisms, the logged-in user has valid Kerberos credentials in their cache, and the appropriate SASL GSSAPI libraries are installed on the client machine, then it will use Kerberos for authentication.

The installation of the SASL GSSAPI libraries was what initially tripped me up. At the time of writing, the documentation does not appear to mention this. Since Pidgin does not know that you want Kerberos authentication for an account, no error is generated if they are not installed; it simply tries to use one of the other authentication mechanisms available.

On Debian and its derivatives (e.g., Ubuntu), this can be resolved by installing the libsasl2-modules-gssapi-heimdal or libsasl2-modules-gssapi-mit package, as appropriate for the Kerberos implementation on your machine.

Adding local JARs to Clojure projects in Leiningen 2

I’ve been working through Amit Rathore’s excellent book Clojure in Action, and was excited to start tinkering with accessing HBase from Clojure. However, I discovered that this was not straightforward.

Amit notes that “you’ll need to have the HBase JAR file on your classpath”. If you’re using Leiningen, it handles downloading your dependencies and adding them to your classpath, which is quite handy. I added the following to the :dependency item in my project definition in project.clj:

[org.apache.hbase/hbase "0.92.0"]

did a lein deps, and thought I’d be on my merry way.

Unfortunately, I promptly ran into a wall: attempts to instantiate the HBaseConfiguration class yielded a “NoClassDefFoundError” for org.apache.hadoop.conf.Configuration. I took a quick look at the JAR that Maven had downloaded for HBase, and sure enough, it didn’t contain that class. I had no problem downloading and running the HBase server, so I took a look through the JARs in the lib/ directory in that, and it turns out that this class is provided by hadoop-core-1.0.0.jar. Apparently the HBase project in Maven Central is missing a dependency.

Leiningen really wants all the JARs that your projects rely on to be managed by Maven; it doesn’t seem that there’s an easy way to just say “include this JAR”. However, it turns out that it’s not terribly difficult to create a local Maven repository. I followed the steps that I found on Paul Gross’s blog, adding -DcreateChecksum=true as suggested in the comments, to create a repository containing hadoop-core-1.0.0.jar, and I was up and running after another lein deps.

Installing a Clojure development environment on Ubuntu

After a not-terribly-thrilling experience upgrading to Ubuntu 11.10 (Oneiric Ocelot) on my laptop, I’ve decided to leave my desktop running Lucid Lynx for a while longer. However, I’d like to do some Clojure development on a nice, big screen, so I needed to figure out how to get a dev environment set up. It looks like this may be easier in later versions of Ubuntu, but here’s the cleanest way I could find to set it up under Lucid.

First, here’s what you’ll need to do as root to get packages installed system-wide:

  1. Install Clojure: apt-get install clojure
  2. Install SLIME.

    If you’re new to Lisp, SLIME is a very powerful Emacs mode for developing Lisp programs. It’s in the APT repositories as well: apt-get install slime

Users will also likely want to be able to easily install clojure-mode, which adds some formatting features and, more importantly, functions for communicating with Clojure processes. To do this, we’ll need to add some package management features to Emacs.

  1. Install package.el.

    Emacs 24 comes with a package management system, package.el, but, sadly, Lucid is only up to Emacs 23. However, there is a version of package.el for Emacs 23 available at http://bit.ly/pkg-el23, which you should download and place into /usr/local/share/emacs/23.1/site-lisp.

  2. Set up the Marmalade repository.

    The default repository used by package.el, ELPA, does not have an up-to-date version of clojure-mode. We want to add the Marmalade repository. To do this for Emacs 23, we will add the following to /etc/emacs23/site-start.el (create it if it does not exist):

    (require 'package)
    (add-to-list 'package-archives
                 '("marmalade" . "http://marmalade-repo.org/packages/"))
    (package-initialize)
    

Finally, we will want to install Leiningen, which, as the README states, is a tool “for automating Clojure projects without setting your hair on fire.” This is very straightforward:

  • Download the zipball for leiningen, and copy bin/lein into /usr/local/bin

That’s all we need to do as root. For each user to set up his instance of Emacs to use clojure-mode, they’ll need to do the following within Emacs:

  1. Update the package list.

    M-x package-refresh-contents

  2. List the available packages

    M-x package-list-packages

  3. Mark clojure-mode for installation.

    Scroll down to clojure-mode and hit ‘i’.

  4. Install the package.

    Hit ‘x’ to execute the installer.

Finally, you’ll want to set up the lein-swank plugin for Leiningen that lets SLIME and clojure-mode talk to Clojure processes.

  1. Bootstrap Leiningen.

    Run lein at your prompt to bootstrap Leiningen, if you haven’t already.

  2. Add the lein-swank plugin to the :user profile.

    Create a ~/.lein/profiles.clj file with the following contents:

    {:user {:plugins [ [lein-swank "1.4.4"] ]}}
    

At this point, you should be good to go. To test your installation:

  1. Create a test “hello” project.

    At your prompt: lein new hello

  2. Change into the new project directory.

    At your prompt: cd hello

  3. Start emacs.
  4. Try to jack in.

    In Emacs: M-x clojure-jack-in

After a moment, you should have a REPL in a new Emacs buffer!

Update: after setting this up on my laptop, which is running 11.10 (Oneiric Ocelot) and looking at the package list for Precise Pangolin, I’m pretty sure that this guide is still valid. While there is a packaged version of Leiningen, it’s older and doesn’t seem to want to play nicely with the current version of clojure-mode.

Audio under QEMU on Ubuntu

I was trying to set up a virtual machine running Windows XP on my Ubuntu Linux desktop under QEMU/KVM to play Starcraft when I ran into a problem. Even after installing an AC97 sound card in the virtual machine and ensuring that Windows had the right driver installed, I wasn’t getting any sound.

It turns out that this is due to a known issue, but it took a surprising amount of log-file-reading and web searching to find it.

When you set up a VM through virt-manager, the default is to connect to it through VNC. Specifically, virt-manager uses gtk-vnc. gtk-vnc does not support tunneling of audio.

Workaround: delete the VNC graphics device on the VM details pane and add an SDL one instead. This solution has shortcomings of its own, but this will get you sound.

It looks like this was identified as a bug by RedHat a while ago, but it’s not clear what progress has been made on it. Given that recent development on virt-manager has added the option to use SPICE instead of VNC, I would hypothesize that VNC will simply be deprecated in the not-too-distant future. Until then, though…switch to SDL if you need audio.

bordeaux rental cars with driver

BPM/Tempo Analysis Software

While preparing for a DJ set at a recent dance event, I wanted to get tempo information for a bunch of songs I was thinking of playing. After a bit of research and advice from a friend, I found a couple of tools that might fit the bill: MixMeister’s BPM Analyzer, and Potion Factory’s Tangerine!.

BPM Analyzer got ruled out pretty much immediately, as it can’t update metadata in AAC files, and actually managed to hang my Mac—a rare feat.

Tangerine!, as advertised, was much more polished. The iTunes integration was slick, and it could update the metadata on the audio files, and fairly quickly at that. After I ran it, though, I found one problem…it’s not very good at finding tempos. :-/. I ended up having to manually time the playlist anyway. It got a few songs right, and a few more it came up with half the right tempo, but the bulk of them were wildly off. It’s not like these were overly “tricky” songs either: most were swinging jazz or gypsy jazz with a very solid beat. I emailed Potion Factory offering to share my playlist if they wanted to improve their product, but so far no response (over a week now).

For now, it seems, we’re stuck with timing or tapping to find our tempos.