Trying Clojure Again

I first started trying Clojure 4 or 5 years ago with only moderate success and not much dedication. Recently, I purchased the 2nd edition of Programming Clojure and am working my way through it very slowly. The biggest change I’ve noticed is how different the ecosystem is now. Leiningen has drastically changed getting up and running with Clojure. It doesn’t hurt that I do all development on a Mac now whereas 5 years ago it was all Windows.

I’m working my way through the Clojure Koans as well as the book. I go back and forth between reading and writing code, trying to memorize as much of the clojure.core as I can. A comment I saw on Hacker News today was apropos: “I knew how clojure felt and behaved, but IMHO you still have to memorize most of the core library, or most code will look confusing.” The code is just foreign still to a guy coming from C#, Python, Ruby, etc. Though I will say that some of the more advanced Ruby I read is similar in nature, at least to my eyes. The syntax is very terse. The shorthand for anonymous functions alone makes my eyes hurt. I’m sure the more I stare at it, the less that will be.

Today, I started learning several of the predicates in clojure.core including subset?. Part of my strategy is to search the documentation for a new function and try to learn it by writing some code using it. Four years ago, it seemed like knowing the core library very well was key to success as I would write a bunch of lines of code only to later discover that functionality was built in.

One goal of learning Clojure is to finally get the functional mental shift to happen so that my Javascript becomes much stronger. I’m watching the Pluralsight course on Javascript The Good Parts by Crockford along with reading Javascript Allonge. It’s clear that until that functional mental shift happens, a lot of this will remain slightly foreign.

State and Identity

Thinking out loud here. I’ve been rededicating some of my attention to Clojure lately with some basic success. However, coming from a background of object oriented languages focused on imperative programming and mutable state, I’m having trouble really internalizing the concepts in Clojure. I recently read Rich Hickey’s essay Values and Change – Clojure’s Approach to Identity and State and while I understand it from a very high level, the details seem to escape me in some significant way when I think about writing programs in a functional style using mostly immutable data.

The real problem is that I don’t completely understand what I’m not understanding. There is just a fuzzy, nagging feeling in the back of my brain that says “This can never work”, examples to the contrary notwithstanding. As a C# developer, I’m used to just modifying anything as necessary in my programs, adding values to lists, modifying dictionaries, randomly changing object values just to screw with people. Ok, maybe not that last part. But with Clojure, the world is very, very different.

[clojure]Last login: Tue May 4 16:42:01 on ttys000
Bretts-Mac-Pro:~ admin$ clj
Clojure 1.1.0
user=> (def mylist ‘(1 2 3))
user=> mylist
(1 2 3)
user=> (cons 4 mylist)
(4 1 2 3)
user=> mylist
(1 2 3)

Here, I create a list, show the list at the command line, cons another number to the list and then show that the original list is unchanged. This takes some getting used to. Rich recommends the following when coming from an OO language:

In coming to Clojure from an OO language, you can use one of its persistent collections, e.g. maps, instead of objects. Use values as much as possible. And for those cases where your objects are truly modeling identities (far fewer cases than you might realize until you start thinking about it this way), you can use a Ref or Agent with e.g. a map as its state in order to model an identity with changing state.

Conceptually, I can think of a map of maps to model the world but I’ll be damned if I can really accept it right now. I understand the benefits behind the immutable data of Clojure but I look at my day to day programming tasks and just don’t see where it’s important. Maybe that’s just a failure of my imagination. I’m continuing to plug away on learning Clojure, trying to work my way into the Concurrency chapter of Stuart Halloway’s Programming Clojure which has been excellent.

Clojure Highlighting On WordPress

I’ve spent the morning getting highlighting working with WordPress and thought it might be worth the writeup to detail my steps since there was one significant gotcha when using published information.

I’m using the SyntaxHighlighter Evolved plugin which you can search for on the Plugins page of your WordPress installation. Install that plugin first.

Once that’s done, you’ll need to follow the directions for creating third party brushes for Syntax Highlighter. Specifically, you’re going to need to create your own plugin, which is pretty simple to do. Here’s what I did.

First, create a new folder in your plugins folder called clojurebrush. In that folder, create a php file with the following code (feel free to change the details at the top):

Plugin Name: SyntaxHighlighter Evolved: Clojure Brush
Description: Adds support for the Clojure language to the SyntaxHighlighter Evolved plugin.
Author: Brett Bim
Version: 1.1.0
Author URI:

// SyntaxHighlighter Evolved doesn’t do anything until early in the "init" hook, so best to wait until after that
add_action( ‘init’, ‘syntaxhighlighter_clojure_regscript’ );

// Tell SyntaxHighlighter Evolved about this new language/brush
add_filter( ‘syntaxhighlighter_brushes’, ‘syntaxhighlighter_clojure_addlang’ );

// Register the brush file with WordPress
function syntaxhighlighter_clojure_regscript() {
wp_register_script( ‘syntaxhighlighter-brush-clojure’, plugins_url( ‘shBrushClojure.js’, __FILE__ ), array(‘syntaxhighlighter-core’));

// Filter SyntaxHighlighter Evolved’s language array
function syntaxhighlighter_clojure_addlang( $brushes ) {
$brushes[‘clojure’] = ‘clojure’;
$brushes[‘clj’] = ‘clojure’;

return $brushes;


Note that this is the same file structure as the directions from the previous link with the exception of removing the version number from the wp_register_script() function call. That’s the thing that ate up a good chunk of my morning.

Once that’s done, you’ll need the JavaScript brush file for Clojure from Travis Whitton. Dump that into the clojurebrush folder that you created above. Go to your Plugins in WordPress and activate your new plugin. Once that’s done, you should have syntax highlighting in WordPress enabled.

Closures in Clojure and C#

So one of the ways I learn a new language is to take new things from the new language and port them back into the old, familiar one. Eventually, I’ll port an entire application (even though that app may be very small) from the old to the new. I find that I learn things much better this way. Tonight’s example is closures in Clojure and C#.

In Clojure, you can do this:

(defn make-greeter [greeting-prefix]
(fn [username] (str greeting-prefix ", " username)))

This basically creates a closure around greeting-prefix that can be used to create custom ways to greet people. For example

(defn hello-greeting (make-greeter "Hello"))

creates a closure than can be called with

(hello-greeting "Brett")

and get “Hello, Brett”

You can do the same thing in C# though it’s a little more work because it’s a statically typed language. In C#, a small console app looks like this:

namespace ClosureToy
    class Program
        static void Main(string[] args)
            //Class1 one = new Class1();
            Func hello = MakeGreeter("Hello");
            Func aloha = MakeGreeter("Aloha");


        static Func MakeGreeter(string greetingPrefix)
            return (username) => String.Format("{0}{1}{2}", greetingPrefix, ", ", username); ;

The MakeGreeter function is a closure around greetingPrefix that allows you to create custom greeter functions on the fly. Essentially, it’s doing the same thing as the Clojure code as you can see from the main body of the console app where it creates two greeter functions on the fly, one that says hello and one that says aloha. Closures make it easier to abstract common pieces of code in your applications.

More Clojure On Windows

I’m slowly working my way through Stuart Holloway’s Programming Clojure book (and using slowly in that phrase does a terrible disservice to the word) and so far, I’m enjoying it. I had a run at Common Lisp a couple of years ago and ran into a brick wall for the most part (I think football season started). I’m mostly playing with Clojure to get a different perspective on programming apart from my usual day job as a .Net developer.

All that intro aside, I wrote about both starting and stopping the REPL here. In working through the book, you need to have the clojure install directory, the clojure-contrib directory and the code from the book on your CLASSPATH. Instead of modifying that in the environment variables on Windows, it’s a great deal easier (and smarter according to IBM to just modify the CLASSPATH at startup using the command line. This is extra easy if you created the clojure.bat file I wrote about in my previous entry linked above. You can just change the one line in it to “java -cp path_to_clojure\clojure.jar;path_to_clojurecontrib\clojure-contrib.jar;path_to_book_code clojure.lang.Repl”. Once you’ve done that, you’re up and running with the book samples.

Being both a Java dummy and a Clojure dummy, I’ll try to keep posting tips from my foray here.

Exiting The REPL In Clojure

Whenever I’m learning a new language, it always seems like it’s the little things that drive me mad. With that in mind, I’m going to try and post fixes to the little things as I make my way through Clojure. To that point, I rapidly got tired of closing down the command window and starting a new one when I seriously hosed things up. Ctrl-D didn’t work though I think that’s the command for Common Lisp. As usual, Google knows everything and the command is Ctrl-C.

Also, if you’re running Clojure on Windows and want to be able to fire up the REPL from anywhere, create a batch file called clojure.bat in your Windows\system32 folder and put the following line in it:
java -cp c:\clojure\clojure.jar clojure.lang.Repl

Then you can just type clojure at the command prompt anywhere and have the REPL start up in that directory. This is helpful once you start writing larger programs that aren’t located in the Clojure install folder.

Google Is Starting To Scare Me

I wrote a post this morning at 11:10 AM detailing getting Clojure set up on my system and I briefly mentioned that I would probably try both Vim and Komodo as editors. At 3:39 PM, someone had come to my site from Google using the keywords “clojure komodo”. I don’t think I’m ever going to get used to the power of Google and its creepy little bots that document the entire freaking Internet. That’s just over 3.5 hours to crawl my site and assimilate the post from this morning into the Google brain. Yikes.

Getting Clojure Highlighting With Vim On Windows

Toralf Wittner wrote a good syntax file for Clojure but I had a hell of a time getting it to work on Windows, mostly because I’m not very smart and/or I didn’t bother to read the documentation for Vim. So sue me. Regardless, if you want to use Vim to edit Clojure scripts on Windows, save the file at the link above into your vimfiles/syntax folder and call it clojure.vim. The filename isn’t important but it makes it easy to see what syntax files are what. I’m currently just loading the file manually when I code in Clojure using :set ft=clojure and boom, you’ve got nice syntax highlighting.

You could probably set this up to automatically recognize .clj filetypes and turn syntax highlighting on for those but I’m going to stick with this small victory first. At least I learned SOMETHING today.

Getting Clojure Installed On A Windows Machine

This is mostly a public service announcement, I didn’t find a good list of requirements anywhere else. Yes, I know I must be insane to be trying to half-ass learn another language but what else would I do on Sunday mornings? There’s no football and the alternative is hard manual labor in the garden. Shudder.

That’s really it, I expected it to be more work but using the binaries over the source simplified the process even though I went ahead and installed Maven too. The Getting Started page has some links to VIM and Emacs syntax files that will probably help your editing a lot. I may try to do some of the initial work in VIM as well as Komodo and see which I prefer.