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.
The four extensions are related. .clj is Clojure for the JVM,
.cljs is ClojureScript and .cljc is code shared between the
two. .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"))
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)
- 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)
Reference
- The Reader – Clojure’s own description of its syntax
- Reader Conditionals – the guide to
.cljcfiles - edn format – the specification of EDN