Setup Clojure/Emacs (Yet another post)

Yup this is yet another blog post that talks about setting up Clojure with Emacs, why another one ? well I think the Clojure world is changing so quickly that this setup process becomes easier every day, you may find this useful.

Why Emacs ? There are many, many posts that you can find that talk about why use Emacs for Clojure and I don’t think I can add any more to what has already been said.

I used the book ‘Sams Teach yourself Emacs in 24 hours‘ partly cause it was in our office library and no you will not be anywhere close to learning everything about Emacs in 24 hours but it is a very good book.

So lets begin

1. Install Emacs, everyone seems to prefer Emacs over Xemacs so I went with that, for Mac I choose Carbon Emacs.

2. Create a .emacs.d directory in your home folder and place a file called init.el , this is where all the customizations go.

3. Install ELPA it is an Emacs package manager.

4. M-x package-list-packages and install slime, swank-clojure

5. Make sure everything is working by trying out this.

6. For integration with a leiningen project see this page.

And finally here is my cheat sheet for Emacs.


TWIKI sucks: plain and simple, the end. I have had to endure it for the past 17 months and cannot find a damn thing one it plus editing sucks. A colleague finally decided to install a search/index engine on a separate box. I guess I got spoiled using Confluence in my last job, it was the best.

Respect my authoritah: It is imperative to have a decision making authority in every meeting (and not a quiet one either, but someone who will put his foot down) or the discussions never stop. This goes double for email discussions, sometimes I wonder how people can get anything done if they spend all their time in typing up long emails (and yes I have written my fair share of long emails too). Anyway here is a great quote from the book ‘The Mythical Man Month’ – “We finally decided to implement the language unchanged and unimproved for the debates about the language would have taken all our effort“.

Perfect is the enemy of done: Along the same lines as above, I have seen many spend all their time and effort revising and redoing to achieve the perfect Object Oriented Design and at the end of the day have nothing to show for. Another quote, unfortunately don’t know where I read it, “A painting is perfect when you cannot take anything away from it” or in other words know when to stop. Although one should strive for good design, that should not be your end goal. The end goal should be ‘getting things done’.

Importance of having a testing team: Even if you have 100% coverage (I think that it is a myth). You need someone whose whole job is to break your system, there is ton of literature on the importance of an independent testing team but unfortunately I keep seeing many places where it is done very less or totally absent. Also I think it is equally important to load test the application before deploying it.

Nice Presentation on F#

If you are studying Clojure or for that matter Scala or any other functional programming language I highly recommend that you check out this presentation “Introduction to Microsoft F#” You can follow along and try out his examples in your own language, the talk is funny with a lots of information that could apply to any functional language. I was really impressed by the F# ide, man I wish Clojure has the same level of IDE support.

Drools Rules!

For anyone contemplating using a Rules Engine here is some free advice. In my career I have had the chance to use three different Rule Engines (ILOG, QuickRules and JBoss Rules),  of all the three I have found JBoss Rules the best, I love it!

JBoss Rules used to be a standalone project called as Drools before it got merged into JBoss, the beauty of Drools was it was simple, no fancy tools or interfaces, just a plain old jar file that you included in your classpath and started using. And best of all it was and still is free, so less of a battle with management.

JBoss Rules works with POJOs and integrates well with Spring and best of all you could learn it quickly. Performance wise, I don’t remember the exact numbers but we did have over 100,000 different rules and it would go through them in seconds. There was never a problem on that end.

It has been over a year that I have used it and when I was using it, it did not have any fancy interface, so we built a rudimentary interface for loading new set of rules into a running application and it worked out very well.

All the rules are stored in a place called ‘Production Memory’ I just call it a blueprint, every time you just make an instance of this blueprint, assert the facts into it, get results and throw away the instance. Creating an instance was very fast and lightweight, and while there are instances floating around in your app you can update the blueprint and the next time an instance is created the updated rules would be used.

JBoss Rules gives you many options for writing rules, you can either write em using spreadsheets (also called Decision Tables) or write them using the provided DSL. Spreadsheets are really good if you have small number of columns, I’d say as long as they fit your screen you are good, once you have to start scrolling vertical, debugging gets a little difficult.

NOTE: do not let your business people edit the spreadsheets, if you have to, give em a website where they can upload and verify it. Regardless of what JBoss says, these excel sheets follow a strict convention, one minor formatting error and you will be in trouble. I wrote a simple program that loaded these spreadsheets and verified if it worked before doing anything else.

Testing is a must, sorry but you cannot get away from this. Based on your data (or facts) many different rules can get active and then unless you specify you own Conflict resolution strategy it will use the default strategy and you may get some unexpected results. This was probably the most tedious part of using a Rules Engine. Also things like ‘OR’ and ‘AND’ work  different that what you are used to. The more rules you have to more testing you will need to do. If there was a wish list for JBoss rules features somewhere I would say a rules coverage feature would be nice to have.

I have been told ‘Jess in Action’ is a very good book to read if you wanted a good introduction to Rules Engines and it tells you how to use Rules, like something I have heard is you should use Rules Engines to get the result and then apply the result to your data and not let the Engine itself modify the data.

Anyways that was my brain dump on Rules Engines.

Uploading and Serving files with Amazon S3/CloudFront and Rails

The attachment_fu plugin written by technoweenie takes care of uploading and retrieving files using S3 and CloudFront, it has other options too such has saving to the database, local file system, rack space. Anyways in this post I describe how to use the plugin with S3 and CloudFront

  1. Create a sample rails project.
  2. Install aws-s3 gem, ‘sudo gem install aws-s3‘.
  3. Install attachment_fu plugin “./script/plugin install git://“.
  4. Assuming that you already have an Amazon S3 account, create a new S3 bucket.
  5. Next step is to sign up for the Amazon CloudFront, this is Amazon’s CDN and like any other AWS you pay for what you use, in addition it integrates with your S3 buckets.
  6. Now log into the CloudFront’s console and create a new distribution channel that is backed with the S3 bucket created in above steps.
  7. Rename amazon_s3.yml.tpl to amazon_s3.yml (it will be in your config folder)
  8. Edit the amazon_s3.yml file and fill in the appropriate information.
  9. Make a model ‘./script/generate model file_meta_data size:integer content_type:string filename:string’ .
  10. Edit the newly created model and add the following lines to it.( ‘has_attachment’ and ‘validated_as_attachment’ are provided by the plugin, there are many other options that you can specify to read more on the options refer to this page).
  11. has_attachment   :storage => :s3,
    :cloudfront => true
  12. Generate controller ‘./script/generate controller Upload index show new edit create update destroy’.
  13. Edit the Upload controller and add the following
  14. def index
      @fileMetaDatas = FileMetaData.all
    def new
      @fileMetaData =
    def create
      @fileMetaData =[:fileMetaData])
        flash[:notice] = 'File was successfully created.'
        redirect_to :controller => :upload, :action => :index
        render :action => :new
  15. Edit the new.erb.html file under the upload controller folder and add the following
  16. <form_for(:fileMetaData, :url => upload_file_path, :html => { :multipart => true }) do |f| >
    Upload A File:
    <%= f.file_field :uploaded_data %>
    <%= submit_tag 'Create' %>
    <% end -%>
  17. Edit the index.html.erb file under the Uploads controller and add the following
  18. File List
    <% for fileMetaData in @fileMetaDatas -%>
    <%= link_to fileMetaData.public_filename,fileMetaData.public_filename %>
    <% end %>
  19. Edit the Routes and add this new route “map.upload_file ‘/new’, :controller => ‘upload’, :action => ‘create’
  20. Run migrations
  21. And run the server, that’s it now you browse to http://localhost:3000/upload/new to upload a file.

Loving Clojure

I seem to be liking Clojure …

Let me backup a bit, in the last couple of weeks I have been debating between picking up Scala or Clojure (don’t get me wrong Ruby is still my favorite).

I always wanted to pick up a functional programming language so I dabbled a bit with Erlang and Haskell, liked Haskell a lot but without much practice it kind of died (sad times)  and Scala seems too much like Java, yeah I know it seems to have a bigger crowd than Clojure and there are a lot of big names behind it.

Maybe that’s exactly why I choose Clojure (since its the underdog), or cause it is different enough from Java or simply cause it has a better syntax and seems more elegant (apparently Clojure has better integration with Java, don’t quote me on it), anyways I decided to learn Clojure.

Peepcode has a nice screen-cast to get you started off on Clojure. If you are on the Mac there is a nice bundle for TextMate and anywhere else Netbeans with the enclojure plugin seems to be the best.

On a side note it seems more and more that Netbeans has the latest and greatest plugins for everything, then comes IntelliJ and finally eclipse, what’s going on with eclipse ? has it reached its peak and now it will start dropping off ? but on the flip side there seems to be more and more apps built on top of the Eclipse RCP like Xmind, so is Eclipse no longer going to be the leader of the IDE and just become a platform for building RCPs. This of course depends  on what Oracle is going to do with NetBeans, I really hope they give the same amount of love to NetBeans as Sun did.

Ok getting back to Clojure, don’t get your panties in a bunch when you see all those parenthesis, it is just the layout that is shocking, indent it well and it is no more than what you are used to.

Here’s an example

(defn fac
"Returns the factorial of n, which must be a positive integer."
(if (= n 1)
(* n (fac (- n 1)))

Is same as

(defn fac [n] (if (= n 1) 1 (* n (fac (- n 1)))))

But the first one is a lot more easier on the eyes (even brain?) than the second one. Most examples that you see look like the second one and it frightens people, don’t let that stop you take my word and go for it.

Clojure seems to be very easy to pick up, things seem very intuitive, like the other day I was wondering, how to return a default value from a map if the key is not found and there is was right there in the api.

(map key default-value)

So simple! I was easily able to extend the examples that came with the peepcode screencast. Anyways I have started on this path, let’s see where it goes.

Update 2009/12/03

– Looked at the Clojure source code, looks squeaky clean, I applied to become a member so that I can expand on the test coverage, hopefully they will accept me.

-It is (load-file “hello.clj”) and not load-file “hello.clj” , I keep forgetting that and after a few mins I realize it.

Language wars are the new IDE wars

(I feel really sorry for Managers, every programmer has his/her favorite programming language and is trying to sneak it into the system. And you know what, it is already in your code base, sorry but that’s the truth and whatever side you take, you will end up loosing. )

I think people used to have IDE wars cause they only had one primary language to work with, but now with the explosion of languages and almost all of them having some port that runs on the JVM everyone is either trying to sneak it in or advertise the virtues of using it. And that ultimately results in a passionate email war.

And of course if you have, somehow magically gotten past that there is always the discussion on the best IDE for that language, hehehe let the wars continue.

Integrating Facebook Connect into your Web App

Lately I have been spending time researching on how to integrate with facebook connect. There is a ton of documentation on the facebook wiki page, but as with any wiki you need to know what you are looking for. I am going to document here what I found.

For the really impatient (like me) check out this video.

  1. To begin with go to this Wiki page on facebook to get familiar with facebook connect and reasons for using it.
  2. Install the facebook developer app into your facebook page (makes it easy to keep track of your api keys and your webpages ).
  3. Next on the facebook developer app register your domain and it will generate  the api key and secret (don’t worry about saving it you can always look it up from the developer app).
  4. Once you have done that you will need to download and place this file xd_receiver.htm into your applications root folder. (For step by step instructions to do this go here.)
  5. Include this tag in every page that you plan to use facebook connect on.
    <script src="" type="text/javascript"></script>    
  6. And also include this snippet to initiate the connection.
    FB.init("your key", "/xd_receiver.htm")

    Should be in the body not head

  7. Place the login button
    fb:login-button length=’long’ onlogin="successful_login();"
  8. You need to create the function successful_login, that will do something when facebook authenticates the user. I just went and pulled the user’s picture and show it. The code for that is here
    $("#login").html("<fb:profile-pic uid='loggedinuser' facebook-logo='true' > </fb:profile-pic>. Welcome,  <fb:name uid='loggedinuser' useyou='false'></fb:name >.");

And that’s about it, if you want you can check out my example at

TextMate Love

I love TextMate, working in it feels no natural. Although I have been an avid VI user, more and more nowadays I keep opening up TM. And it would be so nice if TM were available on Ubuntu.

And yeah trying to make gedit act like textmate sucks donkey b****

UPDATE: Ok so I tried netbeans and also ruby mine and I still think TextMate is the best.