The design of Clojure

The design of Clojure

An outline of how the Clojure programming language is designed to be used.

39f90a6c0ffe4995fb9dff4fb6b6bad6?s=128

Shantanu Kumar

February 28, 2016
Tweet

Transcript

  1. The Design of Clojure Shantanu Kumar, Concur 1

  2. Who am I? • Engineer @ Concur • Author of

    “Clojure High Performance Programming” • Open Source enthusiast: https://github.com/ kumarshantanu • @kumarshantanu on Twitter 2
  3. Clojure • Created by Rich Hickey, 2007 • Eclipse Public

    License (OSS) • Website — https://clojure.org/ • Lisp • Functional • Hosted on JVM (and CLR, JS) 3
  4. Big ideas • Pervasive immutability (Epochal time model) • First-class

    values • First-class functions • Performance • Host interoperability • First-class state management 4
  5. –Stuart Halloway “Clojure feels like a general-purpose language beamed back

    from the near future.” 5
  6. Getting started with Clojure http://leiningen.org/ https://learnxinyminutes.com/docs/clojure/ http://www.braveclojure.com/ https://www.4clojure.com/ 6

  7. Interactive REPL 7

  8. Data types • Number: 42, -5.67, 22/7 • Boolean: true,

    false • Character: \x, \y, \newline, \space • String: “foo”, “bar” (multi-line literals are supported) • Symbol: ‘do-something, ‘bizfn • Keyword: :red, :blue • Nothing: nil 8
  9. Data structures • List: ‘(“Harry” 34 :male) — sequential access

    • Map: {:name “Harry” :age 34} • Set: #{:red :blue} • Vector: [“Harry” 34 :male] 9
  10. Working with data structures 10

  11. Functions 11

  12. Code is data 12

  13. Persistent Data-structures • Immutable (updates return new instances) • Structure

    sharing (Hash array mapped trie) • 32-way branching for O(<7) performance • Sustained performance across updates • Reference: “Ideal Hash Trees” (Phil Bagwell) 13
  14. Working with collections 14

  15. I want a loop! No automatic tail-call optimization 15

  16. –Alan J Perlis “A language that doesn't affect the way

    you think about programming, is not worth knowing.” 16
  17. Records 17

  18. Records • Class semantics (memory layout) • Behave as maps

    • Immutable in the same way as maps 18
  19. Protocols 19

  20. Protocols • Behaviour contract • Polymorphism • Dispatch on target

    object type • Used as implementation details 20
  21. No structural inheritance! But, there are value hierarchies Image source:

    https://xspblog.files.wordpress.com/2008/06/owl_yarly.jpg 21
  22. Macros 22

  23. Sample output 23

  24. Macros • Compile-time code manipulation • Because code is data

    24
  25. Java interop 25

  26. Concurrent execution • CPU-bound and I/O-bound thread pools • `future`:

    Asynchronous one-time access • `pmap`: Concurrent CPU-bound processing • `promise/deliver`: Single producer-consumer • Java’s concurrent collections considered idiomatic 26
  27. Concurrency and state • Atoms — uncoordinated compare and swap

    • Agents — asynchronous sequential operations • Refs — Software Transactional Memory • Dynamic vars — Thread-local access 27
  28. Thank you! @kumarshantanu 28