Friday, June 17, 2011

Simply the best* way to learn and hack Clojure

My previous blog created little bit controversy about the term "serious Programmers" that I have used in the title. So I am putting * after the term "best" to indicate that it is my personal opinion and readers can define their best way of doing the same.
Most of Clojure hackers agree that starting with Clojure can be frustrating. This has been discussed in google groups and elsewhere. Almost all hackers agree that
Clojure programmers spend most of their time in the REPL.
I find many different opinion about how to simplify this process. There are people
just complain about how hard it is while there are people outright defy and
provide all sorts of differing approaches, which adds to a newbie's confusion.
I actually created a project called ClojureW to help newbie get started easily and
quickly just to get a taste of Clojure (I blogged about where and how to get it in
my earlier blogs). This post is about a nice trick that I found to help with Clojure
hacking. In your .emacs file just put following:
(defun eshell-execute-selection ()
  "Insert text of current selection or clipboard in eshell and execute."
  (interactive)
  (require 'eshell)
  (let ((command (or (buffer-substring (mark) (point))
         x-last-selected-text-clipboard)))
    (let ((buf (current-buffer)))
      (unless (get-buffer eshell-buffer-name)
        (eshell))
      (display-buffer eshell-buffer-name t)
      (switch-to-buffer-other-window eshell-buffer-name)
      (end-of-buffer)
      (eshell-kill-input)
      (insert command)
      (eshell-send-input)
      (end-of-buffer)
      (switch-to-buffer-other-window buf))))

and map it to a shortcut key of your choice like this:
(global-set-key [f10] 'eshell-execute-selection)

Then start a Clojure REPL in eshell and type anything in a scratch buffer. You can
select any text and hit F10 to evaluate. One excellent side effect of this hack is
that you do not need to mess with SLIME or any other language specific set up. It
works with any command line tools you already familiar with. You can execute shell
scripts, Ruby scripts, and GNU Smalltalk scripts. If you have any frequently used
commands, it will be perfect to keep them in a file and just select and hit F10.

I found this approach very helpful when I need to experiment with Clojure or Java
libs or platforms such as Java Packages and Incanter(In case, if you are not heard
it is a R like statistical charting platform written in Clojure).

I hope someone finds this useful and Please share if you have handy hacks that ease the pain of hacking. Enjoy!

Disclaimer: I did not write the whole elisp code. I just modified it to work with any selection or clipboard content. I believe I found this elisp snippet somewhere in Emacswiki, a fantastic place to "steal" snippets to hack with.

3 comments:

  1. The controversy about your previous post isn't about your definition of the word best - everyone knows this is your opinion. The controversy here is that you made a monumental statement (that a relatively unpopular language was "best" for something), and then didn't even bother to back it up. If you're going to say something is the best, you should give examples showing why. In this post and the last one, you're not giving any example of why this is better than anything else, and therefore you can't lay a claim to "best" in any sense of the word.

    Please take a creative writing class. You have a good point, but you aren't writing a compelling argument in support of that good point. I'm not even sure you're bothering to support your argument at all actually. Why is the use of immediate mode such a good way to learn to hack Clojure? Why are the particular tools you suggested part of the "best" way?

    Your title is "Simply the best way to learn and hack Clojure" - somewhere in the body of your post, you need to address every one of those words. What is the best way? Why is it simple? Why is it better than other methods? Why is this a good method for learning, and why is it a good method for hacking?

    Make your points, back them up, then reiterate/conclude.

    ReplyDelete
  2. Thanks for taking your time to comment. It really helps me to try to write better next time. Creative writing class has always been on my list. I might take one or two:).
    It is just that I think aforementioned Emacs' set up works the best for my Clojure hacking such as experimenting with Java libs, classes and one off scripts. I just hope that someone finds it useful. Please keep good comments like this coming..

    ReplyDelete
  3. I'm a newbie an I have been reading programming unix linux perl omg it just look like tough knowing where to start is killing me any advice o an c c++ I know is start run cmd a couple meger hacks nothing huge

    ReplyDelete