I’ve been taking a look at Clojure lately and I stumbled upon this post on Stackoverflow that indicates some projects following best practices, and overall good Clojure code. I wanted to get my head around the language after reading some basic tutorials so I took a look at some “real-world” projects.
After looking at ClojureScript and Compojure (two the the aforementioned “good” projects), I just feel like Clojure is a joke. I don’t understand why someone would pick Clojure over say, Ruby or Python, two languages that I love and have such a clean syntax and are very easy to pick up whereas Clojure uses so much parenthesis and symbols everywhere that it ruins the readability for me.
I think that Ruby and Python are beautiful, readable and elegant. They are easy to read even for someone who does not know the language inside out. However, Clojure is opaque to me and I feel like I must know every tiny detail about the language implementation in order to be able to understand any code.
So please, enlighten me!
- What is so good about Clojure?
- What is the absolute minimum that I should know about the language in order to appreciate it?
11
For the background you gave, if I may paraphrase:
- You’re familiar with Ruby/Python.
- You don’t see the advantages of Clojure yet.
- You don’t find either Lisp or Clojure syntax clear.
…I think the best answer is to read the book Clojure Programming by Emerick, Carper and Grand. The book has numerous explicit code comparisons with Python, Ruby, and Java, and has text explanations addressing coders from those languages. Personally, I came to Clojure after building good-sized projects w/ Python, and having some Lisp experience; reading that book helped convince me to start using Clojure not just in side-projects but for professional ends.
To address your two questions directly:
- What’s so good about Clojure? Plenty of answers on this site and elsewhere, e.g. see https://www.quora.com/Why-would-someone-learn-Clojure
- What is the absolute minimum that I should know about the language in order to appreciate it? I’d suggest knowing the big ideas behind Clojure’s design, as articulated in both Clojure Programming and The Joy of Clojure books-wise, and in Rich Hickey’s talks, esp. the talk Simple Made Easy. Once you know the what/why you can then start understanding the how when reading Clojure code, esp. how to change your thinking from classes, objects, state/mutation to “just functions and data” (higher-order functions, maps/sets/sequences, types).
Additional suggestions: Lisp’s elegance and power is partly from its minimalist and utterly consistent syntax. It’s much easier to appreciate that with a good editor, e.g. Emacs with clojure-mode and ParEdit. As you get more familiar with it, the syntax fades away and you’ll “see” semantics, intentions, and concise abstractions. Secondly, don’t start by reading the source for ClojureScript or Compojure, those are too-much-at-once; try some 4clojure.org problems, and compare solutions with the top coders there. If you see 4-6 other solutions, invariably someone will have written a truly idiomatic, succinct FP-style solution which you can compare with a clumsy, verbose, and needlessly complicated imperative-style solution.
0
Lisp dialects have a unique niche due to their simple syntax. This simple syntax makes meta programming through the use of macros very simple. This allows you to model the language to suit your problem domain instead of modeling to fit your problem domain into your language. It’s really cool once you get your head around it, and it lets you do a lot of things easily that is very difficult in other languages.
The downside, is that since every Lisp program essentially defines its own language, you really need a solid grasp on things to understand what is going on.
Now, to answer your questions…
-
Clojure is a modern Lisp dialect that runs in the JVM/CLR. This makes it easy to take advantage of the power of Lisp in an existing infrastructure.
-
It is essential to understand how to use macros to build Lisp to your problem domain to appreciate its usage. I really only understood this after implementing a Lisp runtime, when I found that much of the core language features can easily be implemented in Lisp itself. It takes a fair sized project to get this.
Its pluses aside, although it is fun to program in, I personally don’t have many cases where I would turn to it for daily problems. It is a very good tool to learn for mind expansion, and after learning Lisp properly, it became much more natural to take advantage of meta programming facilities in other languages.
The simple syntax makes it easy for computers to interpret (allowing such easy powerful macros), but it makes it very difficult for a human to parse complex expressions quickly.
9
The strengths of Clojure have been highlighted many times, and, sure enough, make for a very powerful language, so I won’t repeat them again here.
I always loved lisp, since the uni days and I was really excited when I discovered Clojure (perhaps the chance of using lisp outside of academia at last!). However as I started learning an uneasy feeling has begun to creep in, though I cannot pinpoint exactly what it is that I don’t like. First of all I would say that coding in Common lisp was easy and fun for me but Clojure has a steeper learning curve. Clisp had a certain beauty and elegance to it. I’m not sure I’ll get the same feeling from Clojure. I’m wondering if that feeling will go away as I get more experience.
I have another guess for my mysterious slight repulsion. I think it might be its great flexibility. For instance you can have structures that are half vector and half map and with a few characters of code you can accept them as parameters, take them apart and process them correctly. Yes, it removes all the boilerplate, but somehow it doesn’t feel right. Is like you never know very well the rules under all those parenthesis, colons, brakets, etc are operating.
For me, I still love the language for all its advantages (primarily, running in the JVM and simplifying concurrency, while being much more powerful, and concise than Java and with less obvious design flaws) but the jury is still out as to whether something could have been improved.