Created by Pavel Klavík
Released by Rich Hickey in 2007 after two years of work. The name Clojure originated in closure of the languages C#, Java and Lisp (CLJ). It is a stable language with a tiny core. There are amazing libraries and tools which increase the productivity. The basic philosophy of Clojure is simplicity. |
Used by companies such as Apple, Facebook, Netflix, WalmartLabs, ...
Instead f(x,y,z) write (f x y z).
The language is hosted on other platforms and we have a direct access to the environment. Therefore, it is straightforward to use an arbitrary library from the environment and on the other hand the native code can use a code written in Clojure. It is easy to add Clojure into an existing project. In many companies, it was sneaked in by some programmer and was later adapted by entire teams.
The original most used version of Clojure runs on JVM. It integrates well with existing Java libraries and on the other hand can be used to create new libraries used directly from Java. It is enought to add clojure.jar into the project.
ClojureScript is the language version which compiles to JavaScript. It uses Google Closure Compiler which optimizes and produces minimal code.
ClojureDart is the version which compiles into Dart. This allows development of native desktop and mobile apps, and use of Google Flutter.
Fast native Clojure scripting environment, a powerful bash replacement.
Consider the following Java code:
It can be written in Clojure in a more clear form:
More informations about Java interop and Javascript interop; npm packages can be used via Shadow-cljs.
(doto (JFrame.)
(.add (doto (JLabel. "Hello World!")
(.setHorizontalAlignment SwingConstants/CENTER)))
.pack .show
(.setAlwaysOnTop true))
The Internet runs on text messages. The formats such as HTTP or JSON are usable by everyone. ORM and other binary formats are not.
An idea of Clojure is to use the same techniques for programming the insides of our systems. So distinct parts of the system communicate via plain data.
Data can be easily printed, analysed, a single set of functions can be applied, the communication is transparent. Transforms are applied on data to change them. The name functional programming wrongly puts the transformations into the fronts, the data are more important.
There is an entire book by Yehonathan Sharvit which explain benefits of representing data with maps and vectors, instead of objects from OOP. This reduces the complexity, makes the system more understandable and easier to change. This book explains benefits of Clojure programming, while explaining it with examples using Javascript.
(def books
[{:title "The Catcher in the Rye" :author "J.D. Salinger" :year 1951}
{:title "Franny and Zooey" :author "J.D. Salinger" :year 1961}
{:title "To Kill a Mockingbird" :author "Harper Lee" :year 1960}
{:title "1984" :author "George Orwell" :year 1949}
{:title "Animal Farm" :author "George Orwell" :year 1945}
{:title "Pride and Prejudice" :author "Jane Austen" :year 1813}
{:title "Sense and Sensibility" :author "Jane Austen" :year 1811}
{:title "The Great Gatsby" :author "F. Scott Fitzgerald" :year 1925}
{:title "Tender Is the Night" :author "F. Scott Fitzgerald" :year 1934}])
This is a React component. See https://reagent-project.github.io/ for other examples.
I'm sorry that I long ago coined the term "objects" for this topic because it gets many people to focus on the lesser idea. The big idea is "messaging"
Rich Hickey: Objects are like marionettes. Whoever having the reference fully controls the object. Nothing works in the real world like this.
POST /cgi-bin/process.cgi HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.tutorialspoint.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 48
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
licenseID=string&content=string&/paramsXML=string
Rich Hickey: This is life sucking ...
Clojure works with values which cannot be changed. But it is possible to efficiently create modified values.
Rich Hickey created these data structures so they are efficient enought for 99% of all applications. Time to reading is 1-2x, time of writing is 1-4x. Often, the program can be in total more efficient which is counterintuitive.
When one say functional programming, most programmers recall Haskell. It had infected academia so students are forced to learn it in university. I personally think that Haskell does big harm to functional programming because it is presented as mysterious and hard to understand. Words such as category theory, monads or functors create the following reaction with most programmers:
Clojure is a practical transparent programming language without similar non-sense ideas.
React is the most popular JS frontend framework, developed by Facebook.
Reagent is a ClojureScript wrapper for React.
Re-frame gives a basic application structure for a Reagent application.
The full state of your application. Usually a deeply nested map.
Events occur based on the user's interactions and the outside world (e.g., a message comes from the server). Each event produces a new app-db from the current one.
Subscriptions compute and cache values from app-db or other subscriptions.
Each subscription is a pure function of its inputs. So they only have to be recomputed when at least one of the inputs is changed.
Therefore a small change in app-db only leads to recomputing a few subscriptions and changing a few components.
The rendering of the page is described in terms of components, in the Hiccup form:
These views are using subscriptions to display data from app-db.