Saturday, June 19, 2010

Ubiquitous note taking with Vim and Dropbox

Finally I found the combination of a best data sync tool called Dropbox and my favorite text editor Vim for ubiquitous note taking. To make it even seamless, I added following to my .vimrc and sharing it with you guys:
map <leader>nn :tabedit ~/Dropbox/YourName/notes/
<leader> here is a mapleader, which is a special variable in vim that you can bind to a key in your vimrc. I use following to map it to ","
let mapleader = "," 
After that you can use comma with the combination of any characters for mapping things that you end up doing a lot. In this case, I mapped it to creating a new file in a new tab within ~/Dropbox/YourName/notes/ folder with whatever name that I will type before hitting the enter key.  Above setting will allow following work flow:
1. Open vim
2. Just type ,nn and enter the file name you want
3. Hit enter and start typing
Creating a new notes could not be more easier and pleasant than this knowing that your notes will be automatically synchronized across multiple computers even on mobile phones. Enjoy!

Thursday, June 10, 2010

On learning idiomatic Clojure

If you hang around in Clojure google groups and Clojure user groups, one topic that keep popping up is this: How one go about learning idiomatic way of writing Clojure code? After reading almost every source that I could get my hands on, I finally settled for this:
  1. Study Clojure.core daily
  2. Read "Joy of Clojure" The Joy of Clojure
  3. Code, Code, and Code in Clojure as much as you can
If you are really serious about Clojure, grab a MEAP edition of "Joy of Clojure" and the book bundle. Here is how I follow above steps: I find interesting functions from Clojure.core and will implement it by adding my- in front of standard functions. Then I search for the forms used within the function in the "Joy of Clojure".  It is quite an enlightening experience to see how the creator of the language structure his code. On top of that, you have an idiomatic Clojure reference book's help within your reach. Boy, it feels like a happy kid with a cotton candy in your hand.  Let me show you by an example:

 (def
 my-assoc
 (fn my-assoc
   ([map key val] (assoc map key val))
   ([map key val & kvs]
    (let [ret (my-assoc map key val)]
      (if kvs
        (recur ret (first kvs) (second kvs) (nnext kvs))
        ret))))) 
(println (my-assoc {} :symbol "assoc")) => {:symbo assoc}

my-assoc here is a Var that holds a reference to a function that named my-assoc, which takes a map and one or more key-val pairs, and returns a map that contains the mapping.  This actually is a classic Clojure style of tail recursive function. Namely, it is an idiomatic way to use function signatures for base and recursive cases. It is also very elegant to use let binding to hold accumulative value that will be referred in multiple places in a block. The use of recur in the tail position is just what it is intended for. It recursively invokes my-assoc function with the correct number of arguments, which in this case
(my-assoc [map key val & kvs])  until kvs are nil, which is one of only two false (nil and false) in idiomatic Clojure.  (first kvs) (second kvs) (nnext kvs) is also idiomatic way of traversing a seq that will save you from unexpected nil punning. (Again, read "Joy of Clojure" for detailed explanation for this). 

My way of learning idiomatic Clojure is very opinionated and might not work for everyone. What is your way of learning idiomatic Clojure? Please share it if you can so a Clojure noob will have a good start.