The productivity
brought by Clojure
Laurence Chen
http://twitter.com/humorless
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.
Most important reasons to love Clojure from survey
● Lisp & REPL-driven development
● Immutable data structures/Functional programming
● JVM & Java interoperation
Part One
● Clojure is a dialect of Lisp
● REPL-driven development
Lisp?
Lisp is a family of programming languages
1. CommonLisp
2. Scheme
3. Racket
4. Clojure
5. Hylang
6. Emacs Lisp - elisp
7. WASM - WebAssembly
S-expression v.s. M-expression
S-expression
● (fn x y)
● (+ a b)
M-expression
● fn(a, b)
● sort(colloection)
● Beating the average (超越平庸)
Test-driven development
● Edit test file
● Edit code file
● Run the program
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?
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
Demo
● Example Editor Command: cqp
● Evaluate at the prompt
Execution time measurement by cqp
Demo
● Example Editor Command: cpp
● Evaluate the current expression
Demo
● Example Editor Command: > )
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
Demo Time
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 ...
Part Two
● Immutable data structures
● Composable functions
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}]
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
Clojure immutable data structure: copy on write
There is no `cloneDeep` in Clojure
● Garbage collection => obsoletes `delete` (Manually track
memory allocation/deallocation)
● Immutable data structure => obsoletes `cloneDeep`
(Manually manage data references)
Function composability in JavaScript is not good
Object/Map
Array
Functions defined on
Object/Map
Functions defined on Array
??
R.map / R.mapObjIndexed
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,
….
map/ into
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
Part Three
● Immutable database - Datomic
database queries
Orders
Excel filesdaily ETL
I want to know the
revene data today.
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.
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
Conclusion
● You can have better Test-driven development.
● You can forget _.cloneDeep().
● You can have better function composability.
● You can have immutable database.
● Thank you
● Q & A
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

The productivity brought by Clojure

  • 1.
    The productivity brought byClojure Laurence Chen http://twitter.com/humorless
  • 3.
    Motivation behind thistalk ● 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.
  • 4.
    Most important reasonsto love Clojure from survey ● Lisp & REPL-driven development ● Immutable data structures/Functional programming ● JVM & Java interoperation
  • 5.
    Part One ● Clojureis a dialect of Lisp ● REPL-driven development
  • 6.
  • 7.
    Lisp is afamily of programming languages 1. CommonLisp 2. Scheme 3. Racket 4. Clojure 5. Hylang 6. Emacs Lisp - elisp 7. WASM - WebAssembly
  • 8.
    S-expression v.s. M-expression S-expression ●(fn x y) ● (+ a b) M-expression ● fn(a, b) ● sort(colloection)
  • 9.
    ● Beating theaverage (超越平庸)
  • 10.
    Test-driven development ● Edittest file ● Edit code file ● Run the program
  • 11.
    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?
  • 12.
    Editor which canjudge 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
  • 13.
    Demo ● Example EditorCommand: cqp ● Evaluate at the prompt
  • 16.
  • 17.
    Demo ● Example EditorCommand: cpp ● Evaluate the current expression
  • 20.
  • 23.
    Editor integration andsemantic 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
  • 24.
  • 25.
    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 ...
  • 26.
    Part Two ● Immutabledata structures ● Composable functions
  • 27.
    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}]
  • 28.
    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
  • 29.
    Clojure immutable datastructure: copy on write
  • 30.
    There is no`cloneDeep` in Clojure ● Garbage collection => obsoletes `delete` (Manually track memory allocation/deallocation) ● Immutable data structure => obsoletes `cloneDeep` (Manually manage data references)
  • 31.
    Function composability inJavaScript is not good Object/Map Array Functions defined on Object/Map Functions defined on Array ??
  • 32.
  • 33.
    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, ….
  • 35.
  • 36.
    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
  • 37.
    Part Three ● Immutabledatabase - Datomic
  • 38.
    database queries Orders Excel filesdailyETL I want to know the revene data today.
  • 39.
    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.
  • 40.
    Immutable database allowstime 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
  • 41.
    Conclusion ● You canhave better Test-driven development. ● You can forget _.cloneDeep(). ● You can have better function composability. ● You can have immutable database.
  • 42.
  • 43.
    Bibliography 1. stackoverflow survey2019 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