Why Clojure?

Just couple of words about #clojure and its benefits for #cloud #software #development

FFL
4 min readJan 29, 2022

Clojure is a relatively new programming language — its just 15 years old (first appeared in 2007). It is a dynamic and functional dialect of Lisp for Java (and not only by the way). Clojure as a Lisp family member treats code as data (it’s also called homoiconic). Apart of this, as a one of most important things it has — Clojure advocates immutability and immutable data structures and encourages programmers to be explicit about managing identity and its states.

Clojure language official logo

Big datasets oriented

It is a functional programming language, that runs on JVM (Java Virtual Machine) (as well as on CLR (Common Language Runtime) and a but tricky on JavaScript platforms).

What does it means? It means that by virtue of its nature we may write clear, safe and (the main thing) well understandable logics while still getting a full access to all diversity of Java ecosystem.

(+ 1 2)
=> 3
(+ 1 2 3)
=> 6
(+ 1 2 3 4)
=> 10

in other words just “apply function to parameters [vector]”. Naturally.

'(apply + collection)
(apply + [1 2 3 4])
=> 10

Java interop

As it was written on JVM, Clojure gives access to all available Java (or any other JVM language) libraries and their frameworks. So it is possible and also natural to call Java code from Clojure, or vice versa.

(every? #(Character/isDigit %) "12345")
=> true
(every? #(Character/isDigit %) "12345z")
=> false

It should be considered that the languages written on JVM have such a great heritage and this is a huge advantage, especially for developing enterprise and/or cloud software.

REPL

Another amazing thing is REPL — it is a secret weapon for Clojure programmers (and all lispers in common) and it is a tool that empowers interactive development and evolves it rapidly. Well designed application may be fully controlled and even refactored in runtime without any of interruptions (not recommended strategy for production for many reasons).

Discourses of REPL Driven Development never end in Clojure community and it has became quite popular as for today. The reason for this is that one can productively and quickly write a code, check it, test it, enhance it and polish it. Repeatedly. The progresses such as finding a bug or adding/removing a feature can be easily carried out through quick feedbacks with almost no price.

Macroses

Second even more powerfull weapon is a native part of almost every Lisp — macroses. The macro system provides you an opportunity to extend the language itself. It is like a function which produces function(s) for application needs, at compile time. Semantic purity of code is just a one of its benefits.

(defmacro ignore-exceptions
"Simple macro which wraps the given expression in a try/catch block and ignores the exception if caught."
[& body]
`(try ~@body (catch Throwable ~'_)))
(ignore-exceptions (/ 1 0))
=> nil
(/ 1 0)
Execution error (ArithmeticException) at user/eval2111 (REPL:1)
Divide by zero

Builtin STM

STM (Software Transaction Memory) ensures that the state of more than one variable can be changed in a coordinated manner and there won’t be any condition like deadlock. It has a similar structure to the database transactions.

A transaction in this context occurs when a piece of code executes a series of reads and writes to shared memory. These reads and writes logically occur at a single instant in time; intermediate states are not visible to other (successful) transactions. The idea of providing hardware support for transactions firstly appeared in a 1986!

It means “No mutexes, Joe!”.

Let’s summarise?

  • Functional approach that gains high level of code quality and dramatically reduces development cycle
  • Almost endless extensibility by forming application centric dialects and built in streams oriented processing approach
  • No filesystem/project patchworks and boilerplates — only elegant, readable and logically consistent code.
  • While Clojure type system is entirely dynamic, recent efforts have also sought the implementation of a dependent type system — it helps reduce bugs by enabling the programmer to assign types that further restrain the set of possible implementations.
  • Full access to a whole Java ecosystem with less effort.
  • Immutability by design — clear and consistent data flow, less bugs, less time spent on development, easy caching.
  • Portability — high level functional dialect not only hides OS arch specifics, but also level up even Java version differeneces. It significally increases final application lifetime and descreases support costs.
  • Builtin flexible data structures enable painless and convenient data processing.
  • Homoiconicity delivers power and flexible tools for implementing business specific tasks in performance and elastic way
  • Interactive development and realtime debugging

I’m pretty sure that I missed out a lot of things worth mentioning here. But tried out to enlight Clojure benefits in easy and understandable manner.

We use Clojure a lot (mostly on backend side), have a lot of libraries (both open and closed source) of different kinds that empower our production systems and help us develop our cloud solutions and make them more and more robust and safe. And to deliver new services for our customers.

And of course we build a future we believe in.

Original publication on LinkedIn

About the author:

Aleks Ilchenko — Mentor, Cloud Architect, CEO. More than 20 successful years in the fields of HPC and Cloud Computing, more than 10 years of solutions in fin-tech, e-commerce, logistics and retail. OSS enthusiast and Cloud evangelist.

--

--

FFL
FFL

Written by FFL

0 Followers

The new age high-tech research lab