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.

Droid Incredible: Free At Last

A week or so ago, I started getting the much-mentioned, not-resolved error on my Droid Incredible: “Low on space: Phone storage space is getting low.” After trying a few quick fixes suggested by some Google searches, I finally rooted the phone last night and got to the bottom of it.

As this ZDNet article notes, the error message is confounding, because it’s only supposed to appear when you have less then 10MB of storage free on your “phone memory”, yet you can see in Settings→SD card & phone storage that there are hundreds of MB free. It turns out that the problem is that the “phone storage area” is divided into two partitions. One of these is approximately 748MB, is mounted on /data, and is where user-installed applications are stored. The other is about 150MB, is mounted on /data/data, and is where applications store their data. What the SD card & phone storage settings page shows you is the partition that is mounted on /data, but what was running out of space was /data/data.

So, how do you figure out what’s taking up all the space?

  1. Root your phone. This is now a trivial matter, thanks to unrevoked3. This will install the very useful ClockworkMod recovery image, as well as adding a “Superuser Permissions” app that will allow you to grant root access to applications that request it.
  2. Install the Android SDK.
  3. Connect the phone to your computer via USB, and enable USB debugging (under Settings→Applications→Development).
  4. Reboot your phone into the ClockworkMod recovery image, either by using adb reboot recovery from the Android SDK, or powering the phone on while holding the volume-down button, then selecting “recovery” from the HBOOT screen.
  5. Mount the data partition. In ClockworkMod, go to partitions→mount /data.
  6. Get a root shell on your phone by running adb shell on your computer.
  7. Mount the /data/data partition by running the following command from the shell you have on your phone: mount /dev/block/mtdblock6 /data/data
    .
  8. cd to /data/data, and use du (du -sk * | sort -n is a useful pipeline) to find out what’s taking up all the space. (As an aside, it’s way easier to poke around the filesystem from the recovery image rather than using su to get root when the phone’s running normally. The set of command-line tools provided in Android is very bare-bones—you don’t even have cp—while you’ve got a well-equipped BusyBox in the recovery image.)

In my case, I found that the com.android.providers.contacts directory was taking up over 100MB, 90MB of which was in a SQLite database. I had no contacts stored locally, so I just mounted the SD card, copied the directory to that for backup/forensic purposes, and deleted it from /data/data. After rebooting the phone and resyncing my contacts with GMail, the space being used in that directory was cut down by an order of magnitude. I do note that I can no longer sync Facebook contacts, but I’m not certain that that is a result of this—that may not have been working before the “low on space” debacle. If this problem re-occurs, I’ll dig into that SQLite DB to try to find out what was taking up all the space. For the moment, though, the problem is resolved.

Now that I have a rooted phone, I’m very tempted to install the leaked Android 2.2+Sense leaked ROM

Windows Aggravation #132

“Security” through buck-passing.

When I try to follow a link to a Word document in IE, I get the following message:

Some files can harm your computer. If the file information looks suspicious or you do not fully trust the source, do not open the file.”

Thing is, I followed that link because I believe the document has information that is relevant to my interests. Now I have to choose between not looking at it, and potentially getting h4xx0r3d by doing so. Neither of these is an appealing option. (See also: the helpful “Security Center” in Vista.)

Instead of spending the time and effort to have Windows warn me before I open a file that’s of interest so that MS can claim it’s somehow “my fault” for getting pwn3d, why on Earth didn’t they fix Word so that simply opening a document isn’t a massive security risk? I don’t have to worry about this crap if I open a Word doc in Pages on my Mac or OpenOffice Writer on my Linux desktop.

It wasn’t so long ago that we made fun of people for believing that simply opening a document could infect them with a piece of malware. Remember the Join the Crew and Good Times “viruses”? Thanks, Microsoft, for making what most technically-inclined folks thought was unlikely possible; “looking for my virus scanner” is sure where I wanted to go today!