Clojure's Core Features, Part 1
In the past few weeks, I’ve been tasked with implementing my tic-tac-toe program in Clojure. Since I’d already written a working game in Ruby, this was really more of an exercise to pick up Clojure and better understand the functional programming paradigm. Let’s start with some of Clojure’s core features.
Clojure is a homoiconic language, meaning that programs are represented by simple data structures. Rather than passing objects into functions and returning a modified version of that object, Clojure deals directly with the data.
Clojure core data structures are also immutable, making concurrency viable. In most programming languages, mutable state makes it very difficult to keep a program’s behavior and data consistent due to memory volatility and multithreading issues on modern systems. Clojure’s documentation also touches on Clojure’s ability to handle state and concurrency, generally a tough combination: “[Clojure] allows state to change but provides mechanism to ensure that, when it does so, it remains consistent, while alleviating developers from having to avoid conflicts manually using locks etc.”
By taking the pressure off of developers, Clojure makes utilizing multithreading (or multiprogramming) and multicore processors far easier than in other languages. This is extremely relevant today, in a time when we’re reaching the limits of processor speeds and have to resort to multiple processors to increase processing bandwidth. Programs (and languages like Clojure) that can utilize these technologies are the future.
Clojure is fast. My Ruby implementation for minimax was far slower than my Clojure version, even without optimization like alpha-beta pruning or depth pruning! When I was first testing my Ruby minimax, it wasn’t uncommon for a test to run for 10+ seconds before I optimized the code. Clojure ran through the game tree so quickly that I was sure something was wrong with my code.
Piqued your interest? Try Clojure in your browser now or work through some Clojure Koans as a primer!