Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The productivity brought by Clojure

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

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

The productivity brought by Clojure

  1. 1. The productivity brought by Clojure Laurence Chen http://twitter.com/humorless
  2. 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. 3. Most important reasons to love Clojure from survey ● Lisp & REPL-driven development ● Immutable data structures/Functional programming ● JVM & Java interoperation
  4. 4. Part One ● Clojure is a dialect of Lisp ● REPL-driven development
  5. 5. Lisp?
  6. 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. 7. S-expression v.s. M-expression S-expression ● (fn x y) ● (+ a b) M-expression ● fn(a, b) ● sort(colloection)
  8. 8. ● Beating the average (超越平庸)
  9. 9. Test-driven development ● Edit test file ● Edit code file ● Run the program
  10. 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. 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. 12. Demo ● Example Editor Command: cqp ● Evaluate at the prompt
  13. 13. Execution time measurement by cqp
  14. 14. Demo ● Example Editor Command: cpp ● Evaluate the current expression
  15. 15. Demo ● Example Editor Command: > )
  16. 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. 17. Demo Time
  18. 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. 19. Part Two ● Immutable data structures ● Composable functions
  20. 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. 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. 22. Clojure immutable data structure: copy on write
  23. 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. 24. Function composability in JavaScript is not good Object/Map Array Functions defined on Object/Map Functions defined on Array ??
  25. 25. R.map / R.mapObjIndexed
  26. 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. 27. map/ into
  28. 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. 29. Part Three ● Immutable database - Datomic
  30. 30. database queries Orders Excel filesdaily ETL I want to know the revene data today.
  31. 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. 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. 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. 34. ● Thank you ● Q & A
  35. 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

×