Blog of Jeff A blog about programming and random other things.

5Jan/10link

Clojure

I've recently picked up on Clojure, a LISP-dialect that runs on the JVM and I absolutely love it; but like other programming languages, it's difficult to convince others to try a programming language other than c/c++ or PHP.

Many friends are in the blub paradox camp: people view programming languages only in the mental model of his/her favorite programming language. Languages more powerful are deemed "unnecessarily complex", where languages less powerful are viewed as inferior.

In the Programming Languages course, we covered functional programming. The person next to me said:

"c++ can do functional programming, it has function pointers."

For someone that ever passed functions without any messy declarations knows that it's just not quite the same. Ruby is particularly nice with blocks, which is syntactic sugar for passing a function to another function:

with_file('myfile.txt') { |line|
   puts line
}

While with_file doesn't really exist in ruby, it can be easily implemented by someone to open a file, iterate it line by line, and close it. On the other hand, c doesn't provide as nice syntax for something like this.

In clojure, syntax is similar, with the (in)famous parenthesizes.

(with-file "myfile.txt" (fn [line] (println line)))

Some people may say that's a lot of parenthesizes, like any other LISP, but they're mostly in different places. Idiomatically, this with-file would be a macro - a function that generates code to compile. A good use of a macro abstracts boilerplate code away: such as the declaration of the function in the example above.

(with-file "myfile.txt" line (println line))

As you can see, lisp deals away with most of the character syntax that other languages have. Macros can do other handy language-abstractions. For example instantiating an object make take several statements:

MyClass obj = new MyClass();
obj.setProperty("value");
obj.doMagic();
obj.checkForErrors();

There's some repetition here. Clojure has interop support with java, and supports java objects without any wrappers. The above code in clojure looks like:

(doto (MyClass.)
  (.setProperty "value")
  (.doMagic)
  (.checkForErrors))

For one more pair of  parenthesizes, you get to avoid typing the type of class twice, and the variable you're operating on. All while maintaining the readability of the code. That's a pretty sweet deal if you ask me.

There are more issues Clojure touches upon. No doubt multi-threading is a growing concern as CPU manufacturers add more cores, but multi-threaded applications are difficult to make. Dealing with deadlocks is a perplexing experience many first-time threading programmers. Clojure's immutable data structures and concurrency semantics ensures no deadlocks. It's quite a different experience than worrying about if your code functions properly.

I'll leave it to you to check out clojure. Clojure's creator, Rich Hickey, is probably the better person to look for the design philosophies of clojure via his screencasts.

  • Share/Bookmark

Recent Posts

Topics

Archives

Following

Links