Archive

Posts Tagged ‘clojure’

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.

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
user=> mylist
(1 2 3)
user=> (cons 4 mylist)
(4 1 2 3)
user=> mylist
(1 2 3)
user=>

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):

<?php
/*
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: http://yourblog.com/
*/

// 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.