Learn Clojure/ClojureScript in a few days. True story!

To be precise, less than two days of the initial investigation before I started writing code. And you can too.

Of course you will not become an expert of any kind, but you can learn enough to make real applications like this: English Checkers Web App. Also you need to have development background in some other languages.

Actually, I do not know who will read my article. Therefore, in some places I can give even basic information for beginners. For example, for those people who is beginner in some particular tool.

So what all of this is for?

In short:

  • Clojure. Is good for Java developers: less code, easier concurrency, etc.

  • ClojureScript. Is great for Web as a substitute for the JavaScript for the projects with complex logic. Just because its concurrency design, its functional essence, its tools for the Web development, because it is easy to learn and to use ClojureScript even by the developer who is accustomed to the imperative programming. And because you may use same code on both server and client side, using great JVM with Java libs instead of the NodeJS on the background. If you want of cource.

First of all

First of all, you need a task. I came across a good task and I’ll share it. You can see (and download) it here – at initial commit of one of my GitHub repositories: https://github.com/FI-Mihej/English-Checkers-Web-App/tree/6905a5b59c2b9426a1ffbce7e989e8122492596f . There is enough information in the Readme to start the research. And the skeleton of a simple but working project greatly simplifies matters. But it is still painful sometimes. So I decided to share my experience. About some sharp edges.

Next. IDE or Editor

Light Table

Yes, there is a LightTable (LT) around. It has definitely beautiful yet simple design. And there is Clojure plugin for LighTable. But. But LT can be really annoying: it even can’t “just highlight” word occurrences in the file, for example. Even some common “find” was introduced only after a few years (!) from the first public opening. And it is just the natural way of that tool. Code investigation with this tool will be big pain. REPL built in to editor? Ha! You may use dozens of another Clojure REPL implementations: Web apps, REPL started on your machine and used from your own browser, console apps, etc. Also I’m installing all my soft to secured locations like Program Files and LT just could not start its own REPL from those location…

Visual Studio Code

Instead I recommend Visual Studio Code (VSC). It has built in Clojure support (it’s not like PyCharm or real Visual Studio of course, but really better than Windows® Notepad or LT). Also I recommend to install one of “Join line” add-ons: it will be really handy to join line with for example Ctrl+J (and you will do line joins frequently).
So in my future talk will mention Visual Studio Code when it goes to code writing.

And yes – about the code writing

Clojure is like a Lisp. So be prepared to (())))((()() to biiig amount of ())(()))(((()))). :) And all those brackets need a tool to not be painful in the work. An there is a such tool in VSC: you just need to select some text and click ‘(‘ button, or ‘[‘ button, or ‘{‘ button. And voila: VSC will turn the text into the brackets! Believe me - you will understand why I so like this feature. But I’ll talk about it later. And now some

Few small tips:

Try to tend to select text from the end rather than from the beginning: there are always big tail of closing brackets at the end of the expression and it is difficult to remember the exact closing bracket while you selecting the text (in contrast to the opening bracket).

It is handy to use split screen in VSC: you may use second “window” (it is not really window – it rather panel, but still…) to find through some distant parts of file you currently working on (“find” is the second windows is independent of “find” of your main window). Or you may use it as a some kind of constant viewer of some distant data structure, etc.

Another important thing. Git and Windows

If you are using Windows and some antivirus tool – than you already know that Git is slow and GUI tools for Git are even slower. VSC, unfortunately is no exception. If you will open folder with Git repo, VSC, as well as other Git GUIs, will start dozen of Git instances. Actually VSC is faster than for example SourceTree (in a few times), but still slow: 30 and more seconds of 100% CPU load after each file saving is not really pleasant. It is better to leave Git to the command line or to the quality tools like SmartGit.

So let’s go! To turn off Git support in VSC you will need to do:

  • Go to File->Preferences->User_Settings. It will open who panels/windows. Left – is a template for all available settings (with their defaults). And Right – your own settings. It may be empty if it is the first time you’ll opened it. Format is JSON. So place all settings in the already available ‘{‘ brackets (see left panel for example). Left panel is searchable by the way (so you may just search for ‘git’ word and skip to the next article section).

  • Place "git.enabled": false, into the right panel, save it (while focus in on the right panel), close settings and restart VSC.

Clojure and ClojureScript

You may google about it by yourself, so I will just mention things important for fast learning.

REPL

To be able to experiment with following code immediately, I recommend to use one of online REPL. Just google clojure repl online or/and clojurescript repl online.

Language

ClojureScript is some kind of subset of Clojure. So ClojureScript tutorials can be used to learn Clojure as well. And some Clojure parts will not work in ClojureScript, but they are easy to identify by trying them (in REPL for example) and by google them. So

Few basic things to begin with

First of all

First of all, as I already mention, Clojure is like Lisp. So it is using S-expression:
(+ 2 3) will return 5 and (str 42) will return string “42”. It is obvious. But it is possible to miss that (+ 2 4 1 14) is also possible (and it will return 21) as well as (str 12 4 53) (will return string “12453”) and (+ "Hello " "my " "world " (str 42) "!") (will return string “Hello my world 42!”).

Next:

“How can I see the program flow?”

You can use (print ) (docs: print) and (println ) (docs: println) functions. For example (println "This is my-var value: " my-var " ;") will print value of some my-var variable. In the ClojureScript your ‘print’ output will go to the browser console (F12 and so on). And it is turned off by default. To make (print ) and (println ) available in your ClojureScript app, you’ll need to turn it on using (enable-console-print!) expression, as is done here on the fifth line of the code: board.cljs. OK? OK.
But how to inject ‘println’ into another functions in the functional language? And this is the

Third possible question:

“How the hell to evaluate few independent expressions in order, one after another without calling one from within another in idiomatic way?”

There is special operator for this purpose: (do ) docs: do operator.
For example:

(do
  (send-something-to-some-server)
  (println “Send - Done”)
  (if (> 3 0) 35 0))

will send something to some server, print “Send – Done”, and whole (do ) expression will return 35 to the calling code (because 3 is greater than 0)

Documentation and tutorials

As I already mention, you may use ClojureScript tutorials to learn Clojure. But you should remember that ClojureScript is running on top of JavaScript (which is dynamically typed), and Clojure is running on top of JVM (which is statically typed). So sometimes ClojureScript rules are not so strict as in the Clojure.

To start you can read this ClojureScript tutorial (at least the first half - which is responsible for the language description): https://www.niwi.nz/cljs-workshop/

Quick and easy overview of Clojure built in functions by category: http://clojure.org/api/cheatsheet .
Same for the ClojureScript: http://cljs.info/cheatsheet/ .

Clojure specifications can be seen here: http://clojure.org/reference/documentation
Most valuable topics for our purpose are:

And also there is nice DOCs site with a handy search: https://clojuredocs.org . You may use it if you need to get reference to for example “do” operator.

After that you may check out next stuff:

And in the end

That’s all for today. Now everything depends on you. Good luck!

Next time I’ll talk about DataScript – in-memory DB which works inside the browser (inside your Web App) and naturally supports complicated queries.