I’ve known of Incanter, a powerful statistics library for Clojure, for a while now. A couple of days ago I set out to get it running nicely, and was successful. Here are the steps I took:

Installing Leiningen

I used Leiningen because I knew it made packages easy to install, and while it is described more often as a build tool than as a package manager, it has a repl command, so I can use it for experimenting with libraries, too. It was at least as convenient as I expected. I’m going to continue using it to set up repl environments.

To install it, follow these simple instructions from the leiningen repository:

  1. Download the script.
  2. Place it on your path and chmod it to be executable.
  3. Run: lein self-install.

That’s it! It uses the Java installation that comes with OS X. I installed this on Snow Leopard, but these instructions should work on Leopard as well. If you replace aquamacs-emacs-starter-kit with emacs-starter-kit, all of the instructions in this blog post should work on other platforms, with some other minor changes.

Opening a Clojure repl with access to Leiningen

To start a new Leiningen project, I went to my home directory and ran:

This creates a directory called incanterplayground with project.clj and a few other files. Only project.clj will need to be modified to get an Inanter repl environment up and running. I opened project.clj and added the repository and dependency information for Incanter:

And then, following the instructions in Leiningen’s README file, I ran lein deps and lein repl in the project directory:

I wasn’t planning on using swank or nailgun. I haven’t been using emacs, and nailgun doesn’t sound very appealing to me, mainly because when I looked at using it with JRuby a year or two ago, it came with security warnings so strong that they seemed to be telling me you don’t want to use this. When I read the above message, though, I felt like I should give one of the two a try. Before I even tried importing incanter, I started working on getting emacs and lein-swank running.

Getting the lein-swank plugin running

The lein-swank plugin is a server, and therefore doesn’t require emacs to be up and running to run. So I tackled getting lein-swank running, and I put off installing SLIME, which I didn’t know how to use, until afterward. I added a dev-dependencies line to my Leiningen project file. Here is project.clj with this added:

I then ran lein deps and lein swank:

(Note: I made this tutorial while running through these steps for the second time, so lein deps is using files that have already been downloaded. The first time you run it, lein deps will show the download and installation progress.)

This indicates that the swank server is running, and is ready to be connected to by emacs.

Getting emacs running and installing packages with ELPA

I used to use emacs, and I had installed Carbon Emacs to try out org-mode. I didn’t check to see if ELPA was already installed, but rather, ran the directions on the ELPA website. This involved taking a piece of code and evaluating it. Then I went to the ELPA package manager, selected the packages listed in the lein-swank installation instructions, and ran the install. I saw a lot of warnings go by.

When I got done, I restarted emacs, and tried running M-x slime-connect. The command, however, wasn’t installed. I searched for help, and found a post from a user who had the same problem. Someone replied to the post saying that he probably had a bad installation of ELPA. At this point I punted and decided to try aquamacs instead. After installing aquamacs, and running it, I got the idea to find an emacs profile to use that would hopefully have ELPA already working. I searched for an aquamacs emacs profile and found aquamacs-emacs-starter-kit.

I downloaded it to my github directory, rather than to my Emacs preferences directory.

I then added these three lines to the end of ~/Library/Preferences/Aquamacs Emacs/Preferences.el (which just had comments in it):

After this, I restarted Aquamacs, saw a ton of warnings fly by, waited it for it to finish what it was doing, and restarted Aquamacs again. I saw more warnings fly by, and restarted Aquamacs again. I may have restarted Aquamacs a third time before it stopped showing a lot of warnings. It takes a little time for aquamacs-emacs-starter-kit to work its magic.

Then I installed the required packages by typing M-x package-list-packages, selecting the packages that I needed or wanted that weren’t already installed, and typing x to install them. I made sure that clojure, clojure-test, paredit, slime, slime-repl, and swank-clojure were selected before hitting x. After pressing x, I waited while some warnings flew by, and restarted Aquamacs again. Then I tried typing M-x slime and then pressing tab to see what the autocompletion showed. There were a bunch of results, so I felt confident that I had successfully installed SLIME. Indeed I had.

Running SLIME

Finally, I ran SLIME. I still had lein-swank running from a previous step, so I typed M-x slime-connect and chose the default host and port. After a second or two, I saw a silly message indicating that it worked.

I poked around the commands, first using tab completion on M-x, and then looking at the the keyboard bindings in the project.clj buffer with C-h b. I opened a new file with C-x C-f playground.clj, and started evaluating expressions in there. You need to be in a window that’s in the slime mode; the *scratch* buffer will evaluate with emacs lisp if you type C-x C-e in it.

Here’s an example session:

If you go to the end of the first line and type C-x C-e, SLIME should print out #’user/x in the status line. If it works, you know it’s evaluating Clojure and not emacs lisp. Go to the end of the second line and type C-x C-e, and then to the end of the last line and type C-x C-e. It prints out 6. This way I know that it’s running them in the same Clojure session. Pretty cool, huh?

Printing out a graph with Incanter

I found this introductory incanter code, pasted it into playground.clj, and started evaluating it.

After I ran C-x C-e on the first line, a new dock icon with a Java logo appeared. I switched back to Aquamacs, went to the end of the second line, and typed C-x C-e again. Three lines showed up in the emacs status bar, describing an object that was returned. I clicked the new dock icon, and voila! A graph showed up.

What now?

Now that I have a nice Emacs, Clojure, and Incanter setup, there are so many different directions I could go. I have something in mind, but I don’t want to reveal it, because in my mind I might feel like I’m done and lose my motivation to do it. If you need ideas, though, one thing you could do is go to the Data Sorcery blog and start trying the examples there.

There are only two hard things in Computer Science: cache invalidation and naming things.

— Phil Carlton

I find that I spend an awful lot of time naming things. More than that, it’s a distraction. I also spend a lot of time organizing directories, and a lot of time trying to recall where I’ve put things.

Cluttered Office

GitHub has made it a lot easier for me to find code online. In fact, it’s so easy that sometimes I just download another copy of a repository rather than search my home directory for one.

As of yesterday, though, the madness is over, because I decided to replicate github’s organization on my own computer. Here are the first two levels of my new, and currently modest, ~/github directory:

As I get more repos downloaded, I think my new organization style will only make more sense. And if I also need a github repo somewhere else, I can symlink it. Also, ~/src is symlinked to ~/github/benatkin.

The idea came when I realized I didn’t have all of my GitHub repos in one place. As you can tell by the tree, I still don’t. Also, I’m putting stuff in my GitHub directory that I may add to GitHub later. If something stays there longer than a week and  I don’t put it on GitHub, though, I’ll move it to the archive.

This new directory structure also has the nice side effect that I won’t need to delete repositories just because I need stuff to be easier to find.  It shouldn’t take long for the number at the end of the tree output to go over 100, and in a while, the number could be in the thousands. I’ll think of it as a partial mirror.

If you do any native iPhone development, check out ASIHttpRequest. You’ll be glad you did. I tried a couple of frameworks, Objective Resource and HttpRiot before using this library. I found that using a library for making HTTP requests feels much more natural to me than using a framework. ASIHttpRequest reminds me of jQuery’s ajax() function.

Perhaps when I’m working with a large number of models I’ll want something like Objective Resource or HttpRiot. If I do, I’ll consider changing one of them to be an interface to ASIHttpRequest, which builds upon lower-level APIs to be more powerful than NSUrlRequest.

At this month’s BoulderRuby meeting, Randall Thomas gave a talk. During his talk, he told us about R, a statistical programming language. I’d heard of it in passing, but hadn’t tried it. After seeing how cool maps could be generated with very little code, I decided to try it out.

I went to the download page and grabbed the latest binary package for OS X. It comes as a .pkg file. I opened it, and the installation process started. Unfortunately, near the end of the installation, it hung, on the Preparing R 2.10.1 for Mac OS X 10.5 or higher (Leopard build)… step. I switched to another application, and ten minutes later, it was still stuck.

I tried googling specifically for the R installer hanging, but didn’t find anything. I started searching for OS X package installer hanging instead. Finally, I saw a bug report about git-osx-installer hanging under similar circumstances, to which Seth Milliken replied saying that it’s caused by iPhone Simulator.

I was running iPhone Simulator, so I tried killing it, and the installer immediately stopped hanging and told me that installation failed. I ran the installer again and it worked.

I have a django project that currently has two apps: one that contains the data for a directory site, and one that provides support functionality for importing the data in the directory site. I want them to be loosely coupled. The support app should be driving the data app. The data app, and its models, shouldn’t be driving the support app.

A problem I ran into is this: I want a record of an import to remain, even if the imported record is deleted. But, since I used a ForeignKey, and Django has cascading deletes, the record of the import would be deleted. I searched for a way to turn off this behavior, and found two options:

  • Clear the ForeignKey reference to the imported record in the record of the import before deleting the imported record
  • Use a ManyToManyField instead

At first, I used the first option. I saw the second option, but skipped it because it seemed overkill. Something didn’t sit well with having to clear a field before deleting, though. So, weeks later, I searched again, and was reminded of the ManyToManyField option.

The ManyToManyField option is a trade-off. On one hand, it fixes my coupling issues. On the other hand, it adds an extra table to my support class, and is overkill for something that only needs zero or one instances of the model associated with it. In this case, I chose the ManyToManyField option.

I’m quite happy with my new choice, and I think I might have chose it earlier if I hadn’t immediately discounted one approach and listed the pros and cons of each approach first.

I like using the excellent FuzzyFinder plugin for vim. It’s usefulness is heavily dependent on keeping the current directory what I want, though, because otherwise I could do a lot of drilling. I found that I needed a better way of changing directories. What I wanted most of the time was a way to change to the directory of the current file.

I found this while searching for a way to switch to the current directory. I’m not sure whether I picked the same mapping as the author.

map ,C :cd %:p:h<CR>

Admittedly, I’m not sure where the :p:h comes from, but it works well. A couple of months after I found this, I made a second mapping for lcd. Additionally, because I want to lcd to the current directory more often, I made lcd the more convenient remapping.

map ,c :lcd %:p:h<CR>
map ,C :cd %:p:h<CR>

A word about lcd: it changes the directory for the current window, not the current buffer. This sounded obvious to me after I figured it out, but prior to then I thought it was tied to the buffer it’s invoked on and buffers spawned from that buffer.

Another thing I’d like to learn is how to change to the global directory when a local directory is set (or remove the current local directory).

After using virtualenv for months, I finally got around to putting my main virtualenv into my bash profile. It’s really simple. All I had to do was add the following to ~/.bash_profile:

# python virtualenv
source /Users/ben/virtualenv/pearl/bin/activate

This worked, but there was one thing I wanted to turn off. The activate script that virtualenv creates adds the name of the current virtualenv to the front of the prompt. This is useful when switching between virtualenv directories, but it’s not very useful to me when I’m on the default virtualenv. So I moved my prompt definition to the bottom of the file, so it gets executed after the source command above.

# prompt (moved after virtualenv so it isn't shown)
PS1="\[\e[31;40m\]\@ \[\e[32;40m\]\w \[\e[36;40m\]\$\[\e[0m\] "

By the way, to keep the prompt I use the most often short, I set my own colors. It’s easy for me to tell that it’s my main shell, and since it’s my main shell, I know the username and hostname associated with it.

JavaScript is a prototyping programming language, and as such, has many different ways to define classes or create objects. If you try to search for a tutorial on object-oriented programming in JavaScript, you’ll find that each tutorial has its own way of doing things. It can be very confusing.

Fortunately, jQuery UI has a widget framework, that makes setting up object-oriented widgets easy. After using it, I wanted to use it for everything, until I find something at least as easy to use as it. I didn’t, however, want to include a large dependency like jQuery UI if I was only using it for its widget framework.

I was glad to find, after a little experimentation with the Build Your Download page, that I can get jQuery UI’s widget framework in a download of only 9.06 kb minified. You can get this by clicking Deselect All and checking UI Core. After that, the theme drop-down box should be set to No Theme.

I made a geocoding widget for Django in a project for a client using this. It simplified my code a great deal.

One thing it does that’s good for projects that might have a lot of custom widgets on one page (which was not the case for this one) is it takes care of using jQuery’s data() method, which stores objects in a way that is less likely to cause memory leak issues in Internet Explorer than storing objects using closures.

How do you react when you find out that a lot of grief could have been prevented by something as simple as maintaining a to-do list? Or following or not following a piece of advice from a friend or colleague? Or doing a little bit more or less yak-shaving? Or taking a break when you need one? Or paying closer attention to Object-Oriented Design?

Are you happy for the learning experience, or do you get angry at yourself because it’s something you should have already learned?

While self-anger can drive a person to change, I think it’s important for programmers to be able to take a step back when it gets to be too much. After all, every passionate programmer knows enough to be successful beyond his or her wildest dreams. Execution is the tricky part.

I’ve talked a lot about it, but I still haven’t taken a proper twitter break. I took a break for about a week. Toward the end of the break, I didn’t think about twitter a whole lot, but I still thought about it. It’s quite a habit.

Making it take a couple of steps to check my twitter account wasn’t enough, so I’m going to try something else—making my twitter account private. One reason for logging back into my twitter account after such a short amount of time was to replace the current tweets on my twitter profile with fresh ones. Now my twitter profile can only be seen by those whom I follow, so that won’t be such a concern this time.

I made it clear in my twitter profile that I’m taking a break from twitter and will still be reading my DM’s. Hopefully people won’t worry about me this time like a few did when I abruptly deleted my twitter account last October.

So, why has twitter been such a problem for me? I think a big part of it is that I’m not in control of my experience. If I was in charge, I would make my twitter account purely a microblog. People couldn’t see who I was subscribed to. I would also change how I manage users and filter and aggregate my feeds. This latter part I can change by writing my own twitter account, which is something I might do at some point.

I’m going to experiment with doing microblog summaries and putting content in a directory. For now I’m putting them on twitter tagged with #blog, copying the content, and expanding them just a bit. The full articles will simply be directories containing an index.php or index.html file. That way I can play around with the layout of individual posts, or make the pages dirt simple if I so desire.

Image editing with php & GD is simple and fun. I removed text from a SlimTimer Fluid icon I found on flickr with it.