Saturday, September 19, 2015

The Joy of Clojure

So I recently started to look at the programming language Clojure and picked up a copy of The Joy of Clojure to help me learn. The book is extremely well laid out and easy to follow provided you have some programming knowledge - however knowledge of Clojure itself is not needed to work through the book.

Once I started to wrap my head around how everything is a function in Clojure, I really started to like some of the choices which were made when designing this LISP-style language. One thing that I find very helpful is the fact that we can chain any number of parameters into any function call. One example would be to add multiple numbers:
 // Java  
 1 + 2 + 3 + 4  
 // Clojure  
 (+ 1 2 3 4)  
In this example, '+' is the function we're calling and we're using '1', '2', '3', and '4' as the parameters to the function. Another advantage of the fact that everything is a function is there is no such thing as order of operations:
 // Java  
 1 * 2 + 3 / 4  
 // Clojure  
 (+ (* 1 2) (/ 3 4))  
Even in this trivial example, we can see the power of having each operation be its own function with its own parameters. In the Java code, we need to remember that there are implicit parenthesis around the '1 * 2' and '3 / 4' to make sure they get evaluated BEFORE the addition. In Clojure, we just use parenthesis to ensure each operation has the proper parameters we wanted.

So far I have only worked through the opening few chapters in the book but am already liking how the language works, looks, and interacts with Java. I have a GitHub repo setup where I am working through the examples and writing comments of things I feel are important. I look forward to working through more of this book in the near future.

No comments:

Post a Comment