Clojure and 5 Things You Need To Know About It

Lisp gets a bad rap.  It doesn’t matter if you’re referring to the speech impediment or the second oldest high-level programming language family still in use today, it has it’s superfans and its haters.

Like many core languages, Lisp has spawned countless dialects, one of which being Clojure.

So, what is Clojure?  And how is it used in modern systems, applications and services?

Clojure does cop a fair bit of heat from those who have either had a less than stellar introduction to it or are stuck in a JavaScript mindset and find the differences annoying to the point of losing their sanity.

The truth is that it’s fine to work in and it feels like a modern programming language. Just like anything, there are many pros, and many cons, but the net result is that you should take Clojure on its merit and give it some love if you haven’t already. There are certain things you can do in Lisp and Clojure that you can’t in other programming languages. You can never have too many weapons in your arsenal, after all.

1 – WHAT IS CLOJURE:

In short, Clojure is a Lisp dialect. It was originally designed by ‘Rich Hickey’ in an attempt to solve the problems he was having when writing situational programs. No matter which programming language he tried, the result was the same – software that required a monumental amount of time and effort just to make relatively small changes to the coding. Not ideal.

The project was a success, as his team enjoyed working within a framework where there were immutable data structures and a custom-designed functional library. By combining the functional programming language that was Lisp and running it on a JVM like Java designed for concurrent programs, Clojure was born.

2 – WHAT CLOJURE IS USED FOR:

Many features of Lisp have filtered down into other programming languages, but there are two that still remain distinctly Lisp – using code as data, and a unique macro system. Clojure is a little different in that it extends the code as data system out beyond listed terms listed in parentheses (s-expressions), and into maps and vectors. By doing this, both maps and vectors are now able to be used in the macro syntax and have literal reader representations.

Lisp data is read by the reader, representing the data structure in all forms. Clojure builds on this by compiling these data structures that represent code, as a part of the process that calls out to macros. As soon as this happens in Clojure, the macro is called, passes the forms themselves as arguments, and then uses the return value of the macro as a substitute for the macro itself.

These macros are functions that are called upon at compile time to perform transformations of the code itself. Since all data is code, and all code is data, the whole Clojure library is an available resource to assist in code transformation.

Basically, you use macros the same way you use functions in other programming languages – to eliminate repetition of terms in the code. In situations where functions are insufficient to give commands to perform a certain task, a macro can do things like control evaluation and generate identifiers that are defined by the user.

3 – WHY CLOJURE IS POPULAR:

The immutable data structure of Clojure means it cannot be changed. For instance, adding a new key to a map will return a new map instead of modifying the old value, while performing just as well as regular data structures in other languages thanks to clever implementation.

This becomes especially handy when writing multithreaded programs. The big gripe with multithreading is resource access synchronization – which can lead to deadlock, where the program isn’t able to access a relevant resources it needs to continue. The net result is that things such as saved or cached data may cause the program to lock every time it tries to access it. Again, not ideal.

The deadlock problem doesn’t exist in Clojure. Immutable data structures mean that the data can’t be changed as it’s being accessed. If a read while writing situation occurs, Clojure handles it beautifully by separating the reference types, labels the changes, and the changed values for later reconciliation.

4 – THE PROS:

CLOJURE RUNS ON THE JVM:

As we already hinted at, a JVM like Java is a very stable and high performing language environment and is a perfect base for Clojure. You write the code once, and it can be used across Mac, Linux, or Windows with relative ease.

JVM also supports high quality garbage collection, all in a standardized, simple, multithreading package. Reader macros also eliminate all those brackets and parentheses.

IT’S NOT LISP:

Clojure has a consistent syntax, operation names and polymorphic functions. There are no weird unreadable names or type specific versions of the same function like in Lisp proper – which incurs a lot of the hatred directly at it.

MISS NOTHING:

As we’ve already covered, immutable data structure is awesome. It just feels right to structure programs as a bunch of small, isolated functions where the data never changes. It’s true that a shared state can be bad for concurrency, but the simplicity and ease of reasoning and testing more than makes up for it.

TAIL, SMAIL:

There are a massive number of options in Clojure, it’s so expressive that you don’t really need to incur head/tail recursive processing to the same degree as other functional programming languages.

DON’T FORGET CONCURRENCY:

Although Clojure is all about immutability and pure functions to make things simpler, there are still plenty of tools for concurrency in the language should you care about that sort of thing. Other languages like C++ add functions for the sake of it, with Clojure it feels as though they are added out of necessity and not just to drive popularity or design.

SAY GOODBYE TO PARENTHESES:

Short and sweet – say goodbye to annoying parentheses!

THE CONS:

CLOJURE RUNS ON THE JVM:

Sometimes what’s a blessing, can also be a curse. Running on a JVM also means no tail recursion or continuations – which can be a deal breaker for some programmers.

TRICKY TRICKY:

So full disclosure, namespace syntax is complicated – there’s no way around it. There are just a few too many keywords to do it, and it feels just a little too “boilerplate-y” even though it’s pretty flexible. There is no hard and fast rule to keep a one-to-one ratio between files and namespaces, but getting into the habit of following this convention is strongly advised.

A LITTLE DIFFERENT:

Unlike Unix and Python where the core is small and there is an abundance of libraries (a system that many programmers prefer), Clojure has a huge number of functions, with many doing very, very similar things only in a slightly different way. It can be a lot to learn and memorize, but then again, that’s what cheat sheets are made for right? No excuses!

CHAMELEON:

Polymorphism is awesome, and the sequencing and collection functions work well in all data structures. The downside is that to work well, the results are always expressed as seqs which may not be ideal for hash maps. The work around is to chain those functions and stop caring so much about the resulting type.

THE UGLY:

Macros are powerful features, but the quoting syntax will always look ugly. You can’t polish a poop, but you can roll it in glitter and make it sparkly. This is the price you pay for being able to manipulate how Clojure processes code.

JAVA, EWWW:

As Clojure runs on the JVM, Java does sneak in a fair bit. It’s the most straightforward language interfacing for a reason – it’s simple, and it’s powerful. Java haters will instantly recognise Java code inside Clojure because… well… it looks like Java code inside Clojure. There will be a need to interact with third party Java libraries to make up for some items that the Clojure libraries lack.

WHO’S USING IT:

Walmart, Boeing, Daily Mail Online, DRW Trading Group, Cisco, Netflix, Citigroup, LinkedIn and Deutchebank are just a few of the companies that have adopted Clojure for use in specific applications. You can’t get more mainstream than Walmart!

WHAT DOES IT ALL MEAN?

We aren’t saying that Clojure is the ideal programming language, and it may even be less than ideal when compared to something like Python, but it’s definitely worth a look. There are specific times where Clojure is the tool for the job, and wouldn’t you love to be able to flex your knowledge when opportunity knocks at your door?

Many programmers are skeptical of Clojure, purely because it’s based of Lisp – a syntax that nearly everyone can agree is a pain to learn. But Clojure is different. Regardless of whether you like it or not, it will definitely give you a new and unique perspective about the way you code and approach problems.

If you’re interested in taking steps to learn Clojure we have decided to help you get started today! In our opinion, these are the 5 best online resources to learn Clojure – a full range from absolute beginner to grisly programming veteran. Most of them are even free!

THE 5 BEST RESOURCES FOR LEARNING CLOJURE:

 1 – ClojureBridge Curriculum:

The ClojureBridge Curriculum is a community-based resource that combines a pool of knowledge in one handy place and is perfect for beginners. Anyone just starting out writing in Clojure should take a look at this before doing anything else.

2 – Clojure from the Ground Up:

A reading that has a very direct approach to Clojure, designed to bring in programmers proficient in other languages and explain Clojure in an engaging way.

3 – 4Clojure / Clojure Koans:

Once you’ve mastered the basics, this website allows you to complete practice exercises in the browser – super useful. Clojure Koans is the next level up, and slightly more difficult exercises that you’ll need a git and an editor installed on your computer to complete.

4 – My Installation Guides:

A handy resource for building applications to run in the three major operating systems – Windows, Mac and Linux.

5 – Clojure Cheatsheet:

Never forget – cheatsheets are you friend and even seasoned veterans have them saved into their browser as a quick reference item. This is an organised list of built-in Clojure functions and macros right at your fingertips – so use it!

NEXT STEPS

We hope this blog helped you better understand Clojure and all the pros and cons of the language! If you enjoyed this blog you may also enjoy our other blogs like, 7 Reasons Why VueJS Is So Popular or 9 Things You Need to Know About Kotlin.

Kofi Group is proud to be a source of knowledge and insight into the startup software engineering world and offers a multitude of resources to help you learn more, improve your career, and help startups hire the best talent. If you are interested in learning more about what we do and how we can help you then get in touch or watch our Youtube videos for additional information.