Introduction
Clojure is a dialect of Lisp created by Rich Hickey and released in 2007. It runs on the Java virtual machine and makes immutable data the default. ClojureScript is the same language compiled to JavaScript. Other implementations run Clojure on .NET (ClojureCLR), in a native interpreter (Babashka) and elsewhere; the dialects page lists them.
The extensions are related. .clj is Clojure for the JVM,
.cljs is ClojureScript and .cljc is code shared between the
two. .cljr is ClojureCLR, .cljd is ClojureDart,
.joke is Joker and .bb is a Babashka script. .edn is not code but data:
Extensible Data Notation, the subset of Clojure’s syntax used for configuration files and
data exchange, such as a project’s deps.edn.
Structure
A Clojure file is UTF-8 text made of forms. It usually starts with an ns
form that names its namespace and its dependencies. A namespace maps to a path, with
dots as folders and hyphens as underscores, so my-app.core lives in
my_app/core.clj.
Beyond lists in parentheses, the syntax has vectors in [ ], maps in
{ }, sets in #{ } and keywords such as :name.
Function parameters are a vector. Comments start with ;, and
#_ makes the reader skip the next form. In a .cljc file,
reader conditionals such as #?(:clj a :cljs b) choose code for each
platform.
(ns example.greet
(:require [clojure.string :as string]))
;; A function with a documentation string and one parameter.
(defn greet
"Return a greeting for a person."
[person]
(str "Hello, " (string/capitalize person)))
(println (greet "world"))
How the viewer reads each file
A file is shown as coloured text, with its own name for the kind of file it is. The text, not only the extension, decides when an extension is shared.
| File | Shown as | Page |
|---|---|---|
.clj | Clojure source file | File structure |
project.clj | Leiningen project file (Clojure code, read by name) | Tooling |
.cljs, .cljc | ClojureScript source file; Clojure shared source file | ClojureScript and .cljc |
.cljr, .cljd, .joke | ClojureCLR, ClojureDart and Joker source files | Dialects |
.bb | Babashka script when the text is Clojure; otherwise a text file with a .bb extension (BitBake recipes also use it) shown without colour | Dialects |
.boot | Boot build file | Tooling |
.edn | Extensible Data Notation file: every form is data | EDN |
deps.edn, bb.edn, shadow-cljs.edn | tools.deps, Babashka and shadow-cljs configuration files (EDN, read by name) | Tooling |
.lisp, .lsp | when the text reads as Clojure rather than Common Lisp, shown as a Clojure source file | Other Lisps |
Not described
Known, not yet covered
- Reference: the sources read were the Clojure reference pages (reader, special forms, evaluation, macros, namespaces, vars, data structures, sequences, lazy sequences, transducers, atoms, refs, agents, transients, datatypes, protocols, multimethods, Java interop, compilation, the CLI and deps.edn, differences with other Lisps), the guides on reader conditionals, spec, destructuring, threading macros, tools.build and the reading of characters, parts of the ClojureScript reference, the edn specification, the release notes
changes.md(1.3 to 1.12.6), and the source ofclojure.core, its libraries and the reader (see the sources note). The rest of those pages, the Learn Clojure pages, the FAQ, the rationale pages, core.async and test.check documentation, Leiningen, Boot, Babashka and shadow-cljs manuals, and the API documentation text of each function were not read, so the pages name functions and do not give every argument list. Each page's own Not described section gives what it leaves out. - Reference: the notes for Clojure 1.0, 1.1 and 1.2,
cljs.coreas a library, ClojureDart, jank's file extension and the Joker and Babashka features were not in the sources and are marked as from memory where they appear. - Viewer: the colouring follows the text and not the program. A local that shadows a library name is coloured as the library name where it is used; a macro the program defines is coloured as a function call unless its name begins
def; the default expression of acase(the last form when the clauses are odd in number) is read as a constant when it is a list; the contents of a regular expression and the escapes in a string are not coloured separately;(comment …)is coloured as code. - Viewer: not followed: aliases made at run time with
alias, reader features and options (*read-eval*,:features), customdata_readersfiles, user-defined binding macros (defn-like macros with their own parameter forms are read as code unless they begindef), the names of Babashka, Joker, ClojureCLR and jank libraries, and the stricter number and symbol rules of edn (an.ednfile is read with the data forms of Clojure's reader, which are a superset).
Unknown
- Viewer: the colouring was tested on hand-written cases for every token form, on 1 MB worst cases and on every Clojure file that could be found on this Mac: the sources of the Clojure language and libraries that were downloaded for this reference, which are real Clojure but all by the language's own authors. No application written with Leiningen or deps.edn, no ClojureScript application and no Babashka script was available, so how the context rules fare on such code (and on large macro-heavy files) is not known.
- Reference: no Clojure, ClojureScript or Babashka installation was available on this Mac, so no example was run; the examples were taken from the reference and checked by reading, not by running.
Links
Software
- Clojure – the compiler and
cljtools (Windows, macOS, Linux) - ClojureScript – the compiler that targets JavaScript (Windows, macOS, Linux)
- Leiningen – project and build tool for Clojure (Windows, macOS, Linux)
- Babashka – fast-starting native Clojure interpreter for scripts (Windows, macOS, Linux)
- shadow-cljs – build tool for ClojureScript (Windows, macOS, Linux)
- Calva – Clojure extension for Visual Studio Code (Windows, macOS, Linux)
- CIDER – Clojure environment for Emacs (Windows, macOS, Linux)
Developers
- tools.reader – the Clojure reader as a library (Clojure)
- clj-kondo – static analyser and linter for Clojure (Clojure)
- SCI – the small Clojure interpreter that Babashka is built on (Clojure)
Reference
- The Reader – Clojure’s own description of its syntax
- Reader Conditionals – the guide to
.cljcfiles - edn format – the specification of EDN
- clojure.core API – every public name of the core library
- ClojureScript reference – compiler options, JavaScript interop and the build