Into The World of Clojure - Joe Norman - Ronald James

Into The World of Clojure – Joe Norman

Into The World of Clojure – Joe Norman

The majority of my time as a professional software developer is spent programming in JavaScript (more specifically, Node.js) and C# (again, more specifically – .NET MVC). When I’m not knee-deep in this mixture, I like to venture into the world of functional programming. And perhaps to discover something new, something completely out of the ordinary. For me, that was Clojure.

My initial investigations into Clojure left me sort of, stumped. The difference even in semantics alone is enough to completely put you off investing your precious spare time into attempting to understand it! What on earth is reverse polish notation? And why is literally everything wrapped in parentheses? Hopefully, this article will help demystify this obscure yet purely elegant language.

As I previously mentioned, almost everything in Clojure will seem downright bewildering to the majority of software developers. With its Lisp-uresque dialect, it truly allows for a more expressive experience, which, with enough time to master, will provide joy to the majority of programmers who are truly passionate about the quality of their code.

To start off, everything in Clojure is evaluated in a uniform structure (or rather, as Forms). That is, it recognises expressions in both literal (i.e defining a map or a vector) and operational formats. For example:

Literal

web developer tools

 

 

 

 

Operational

web development example

 

 

 

 

 

 

A few things to observe:

  1. “Vectors” are the common term for what most developers will know as Arrays or Lists. All data structures in Clojure are also immutable, in that once defined, their state cannot be changed. This is a huge plus to functional programming!
  2. Notice the ordering of operations in the first statement? This is order known as Reverse Polish Notation (see http://www-stone.ch.cam.ac.uk/documentation/rrf/rpn.html). This basically means that operators (such as `+ – / *`) all appear before a sequential list of operands.
  3. Basic types can be passed literally, but also defined by their individual types; to name a few – Number, String, Map or Set.
  4. Common operational commands such as if statements, loops, and variable declaration are pretty self-explanatory if you look at other languages (such as the usage of let) note: the loop above is actually a Clojure Macro (and statements such as if are called special forms).
  5. Creating variables (such as using def) is commonly known as assigning variables in other languages – in Clojure, this is known as binding (see point 1 on immutable values).

Of course, the examples above do not give the language justice in any sense; Clojure has a large variety of native simple types and intuitive data structures.

This is all great and fine for a REPL, but what about actual programs? What do we do when we have a particular piece of functionality in mind? Functions that’s what!

In the operational examples above I also forgot to mention one thing: and that was the calling of the native Clojure function; first. Clojure has a large number of native functions, but what about defining our own functions? Here’s a good basic example of defining functions:

23

 

 

 

 

 

 

As the comments in the example above state, we’re seeing a very simple function being defined, passed parameters, and then returning a result. You can also declare anonymous functions (much like JavaScript for example), using the following notation: “(fn [params] body)” (so just swap defn for fn). Functions in Clojure are also similar to javascript, in that they can, in fact, return other functions and be passed functions as parameters (first-class functions).

Clojure as a language provides a unique way to write complex applications. It provides an elegant, powerful syntax which makes complex applications seem trivial to write. There is a LOT more that can be said about Clojure, but to get started in writing Clojure head to Try Clojure  to start a REPL, and writing awesome stuff! If you’re like me and like to get straight into reading about Clojure, I’d highly recommend Clojure for the brave and true (http://www.braveclojure.com/); a fantastic read to really get to grips (and it’s also free to read online).

 

© Ronald James Ltd. 09824756. Q16 Business Exchange, Quorum Business Park, Newcastle upon Tyne, Tyne and Wear, NE12 8BX, United Kingdom. Website by Outlines Design.