Clojure
- It's a
lisp
(list processor) language - Code is made up of lists
- Every thing in clojure returns a value, therefore lists can be chained
- Clojure
- Clojure separates data from functionality
- Clojure visualizes data
- Clojure code is data
- Clojure collections are immutable
- Clojure is data-oriented
- Functional Programming
- Processes input and produce output
- No change of state at any way!
- Data structures are
immutable
, if a change is required, it must be copied with the desired modifications (copy on modification
) - Immutable by default
- Value Semantics
- Thread Safety
- Null (nil) safety
Clojure
is compiled language and runs onJVM
ClojureScript
is interpreted and runsJavascript VM
\
# Installation
pacman -S clojure
pacman -S leiningen
paru "babashka-bin" # clojure interpreter
Form
-
A
form
(akaS-expression
-sexp
) can be: -
A symbol
- A literal
-
Anything enclosed in brackets of some type
()
,[]
,{}
,""
-
Special form
-
Special forms
compose the base minimum of the clojure language - They behave very similar to functions
Symbols
- Symbols hold
values
,functions
or any other thing - A
binding
is created when a symbol is associated with a thing - Symbols are immutable! That's why they are not called variable
Functions
Arity
: number of parameters that a function receivesHigher Order Function
: functions that take functions as argumentsPredicate
: functions that return true/false (usually end with ?)Pure Function
- Always return the same value for the same input
- Does not effect anything in its environment (e.g., printing, hitting mutating APIs)
- Pure functions in clojure usually are not named with verbs but with what it returns
REPL
Read Execute/Evaluate Print Loop
: quickly test scripts- Connects to the running app
- Modify the app as it is running
- Jack in
- It's the start of REPL along with all the necessary dependencies
- It has built-in configuration for
Leiningen
,deps.edn
andshadow-cljs
projects - For projects, it must have a project file in the directory open. E.g.,
project.clj
(for leiningen),deps.edn
(for deps.edn) - CLJD REPL types:
Figwheel Main
,Lein-Figwheel
(legacy figwheel),shadow-cljs
- nREPL protocol
- It's used to allow the editor plugin (nREP client) to connect to the Clojure process (nREPL server)
- It accepts connections and allows the initiation of a program-to-program dialogue
- It's an asynchronous message-based network protocol for driving interactive development
- More operations can be supported, or existing operations can be modified or augmented, through
nREPL middleware
(e.g., piggieback)
Clojure on vscode
-
Calva extension
-
REPL
-
Ctrl Alt C
+Ctrl Alt J
: start REPL and connect to REPL (project) -
Ctrl Alt C
+Ctrl Alt C
: connect to REPL (project) -
Ctrl Alt O
+O
: show REPL output window -
Debug
-
Ctrl Alt C
+I
: instrument a function to be debugged (you must run the function in REPL afterwards) -
Evaluation
-
Ctrl Enter
: evaluate the current form -
Alt Enter
: evaluate the current top level form -
Ctrl Alt Enter
: evaluate the current enclosing form til cursor -
Shift Alt Enter
: evaluate the current top level form til the cursor -
Ctrl Alt C
+Enter
: load current file into REPL -
Ctrl Alt C
+D
: interrupt running evaluations -
Paredit
-
Shift
+Alt
+Right/Left
: expand/shrink sexp selection -
Ctrl
+Right/Left/Down/Up
: navigate throughout sexp (forward/backward/down/up) -
Ctrl
+Shift
+Right/Left/Down/Up
: select throughout sexp (forward/backward/down/up) -
Ctrl
+Alt
+Backspace
: remove sexp backwards Ctrl
+Alt
+.
: Slurp Sexp Forwards (pull the next Sexp into this Sexp)-
Ctrl
+Alt
+,
: Barf Sexp Forwards (push the last Sexp out of this Sexp) -
Ctrl
+Alt
+P
+R
: replace the enclosing Sexp with the current Sexp -
Formatting
-
Ctrl Alt L
: format the code -
Testing
Ctrl Alt C
,Shift T
: run all tests in the projectCtrl Alt C
,T
: run tests for current namespaceCtrl Alt C
,Ctrl Alt T
: run current test
Clojure on intellij
-
Cursive plugin
-
Paredit
-
Ctrl
+Shift
+I
: Slurp Forward (pull the next form into this form) -
Ctrl
+Shift
+K
: Barf Forward (push the last form out of this form) -
REPL
-
Ctrl
+Shift
+R
: run REPL Ctrl
+Shift
+L
: reload REPL-
Ctrl
+Shift
+P
: run top level form -
Formatting
Ctrl
+Alt
+L
: format the code