Published on
This slides explains why Clojure programming language brings productivity to programmer from the perspectives of:
1. REPL-driven development
2. Immutable data structure
3. Composable functions
4. Immutable database
-
1.
The productivity
brought by Clojure
Laurence Chen
Tweets by humorless -
2.
Motivation behind this talk
● 2019 Stackoverflow survey: Clojure programmers get highest salary
● But, in Taiwan
You will find no job if you tell your boss that you want to use Clojure. -
3.
Most important reasons to love Clojure from survey
● Lisp & REPL-driven development
● Immutable data structures/Functional programming
● JVM & Java interoperation -
4.
Part One
● Clojure is a dialect of Lisp
● REPL-driven development -
5.
Lisp? -
6.
Lisp is a family of programming languages
1. CommonLisp
2. Scheme
3. Racket
4. Clojure
5. Hylang
6. Emacs Lisp – elisp
7. WASM – WebAssembly -
7.
S-expression v.s. M-expression
S-expression
● (fn x y)
● (+ a b)
M-expression
● fn(a, b)
● sort(colloection) -
8.
● Beating the average (超越平庸) -
9.
Test-driven development
● Edit test file
● Edit code file
● Run the program -
10.
Areas of improvement
1. Can we get immediate feedback?
2. Can we write and test incrementally?
3. Can we test even smallest unit? For example,
single expression or statement? -
11.
Editor which can judge the
boundry of s-expression.
REPL-driven development
Write test argument in Editor
Write source code in Editor
Clojure REPL
Send source/test to
REPL
Get result back -
12.
Demo
● Example Editor Command: cqp
● Evaluate at the prompt -
13.
Execution time measurement by cqp -
14.
Demo
● Example Editor Command: cpp
● Evaluate the current expression -
)
“>
15.
Demo
● Example Editor Command: > ) -
Evalute at…”>
16.
Editor integration and semantic editing
● cqp => Evalute at the prompt
● cpp => Evaluate the current expression
● :Require => reload the whole file
● [ d => jump to definition
● > ) => slurp
● < ) => barf
● cseb => surround the current element with parentheses
● dsf => delete surrounding parentheses -
17.
Demo Time -
18.
Q & A
● How much productivity improvement will you have from
REPL-driven development? at least 30%
● Can REPL-driven programming be used in other
programming languages? Yes, but …
● Can macro (meta-programming) be used in other
programming languages? Yes, but … -
19.
Part Two
● Immutable data structures
● Composable functions -
20.
You need `_.cloneDeep()` at javascript
var a = [1, 2, 3]
a.flatMap(z => [z, z+3]) => return value is [1, 4, 2, 5, 3, 6]
var b = [{x: 1}, {x: 2}, {x: 3}]
var c = b.flatMap(z => [z, z[“x”]]) => c is [{x: 1}, 1, {x: 2}, 2, {x: 3}, 3]
c[0][“x”] = 6; => b is [{x: 6}, {x: 2}, {x: 3}] -
21.
Functional programming dilemma
● Passing data by value
○ Guarantee that any changes will only affect local scope.
○ Extremely inefficient
● Passing data by reference
○ Save memory/Fast
○ Code is more difficult to reason about
○ Not safe at multi-thread environment -
22.
Clojure immutable data structure: copy on write -
…”>
23.
There is no `cloneDeep` in Clojure
● Garbage collection => obsoletes `delete` (Manually track
memory allocation/deallocation)
● Immutable data structure => obsoletes `cloneDeep`
(Manually manage data references) -
24.
Function composability in JavaScript is not good
Object/Map
Array
Functions defined on
Object/Map
Functions defined on Array
?? -
25.
R.map / R.mapObjIndexed -
Sequence <-> Transformation library
Set
Map
…”>
26.
Collection <-> Sequence <-> Transformation library
Set
Map
Vector
List
Sequence
map, filter, reduce,
first, rest, mapcat,
apply, take, drop,
…
Lazy sequences,
strings,
Lines in a flie,
Files in a directory,
…. -
27.
map/ into -
28.
Q & A
● How much productivity improvement will you have from
immutable data structures and composable functions?
at least 30%
● Can immutable data structures and composable functions
be used in browser? Yes, ClojureScript -
29.
Part Three
● Immutable database – Datomic -
30.
database queries
Orders
Excel filesdaily ETL
I want to know the
revene data today. -
31.
temporal database queries
Orders/ Orders
history
Excel filesdaily ETL
I want to know the
revene data today.
I want to know the revenue
data last week. -
32.
Immutable database allows time traveling
● orders history table is the analogy of
`_.cloneDeep`
● In Datomic, you only need orders table and
`(as-of db t)`
● SQL:2011 also support temporal databases
● PostgreSQL has temporal_tables extensions -
33.
Conclusion
● You can have better Test-driven development.
● You can forget _.cloneDeep().
● You can have better function composability.
● You can have immutable database. -
34.
● Thank you
● Q & A -
35.
Bibliography
1. stackoverflow survey 2019
2. xkcd.com/297 Lisp Cycles
3. Dmitri Sotnikov — Clojure distilled
4. Rich Hickey — “Clojure Concurrency” talk
5. Juan-Manuel Gimeno — Functional programming in clojure