Clojure

Created by Andrew Scott

What We'll Cover

- Lisp - Data Handling in Clojure

Lisp

List Processing

Code is Data. Data is Code.

Metaprogramming?

Homoiconicity

Homoiconicity Example


(+ 2 4)
; => 6
                

(def example (list '+ (+ 1 1) (+ 2 2)))
; => (+ 2 4)
                

(eval example)
; => 6
                

(read-string "(+ 2 4)")
; => (+ 2 4)
                

(eval (read-string "(+ 2 4)"))
; => 6
                
Why is this useful?

Macros and Clojure

'when' macro


(when (true) (println “hello”))
; => “hello”
                

(macroexpand ‘(when (true) (println “hello”)))
; (if (true) (do (println “hello”)))
                

(defmacro when
    "Evaluates test. If logical true, evaluates body in an implicit do."
    {:added "1.0"}
    [test & body]
    (list 'if test (cons 'do body)))
                

Aren't macros just functions?

Functions Macros
Execute at runtime Execute at compile-time
Take and produce data (values) Take and produce code
Questions

So Clojure is a Lisp... What else?

Data Manipulation Guarantees

- The old version remains accessible - The new version can be produced efficiently - Both versions satisfy the complexity guarantees

How?

- Data structures are hash array mapped tries - Shallow with high branching factors - Structural Sharing

What does this mean?

Structure of your program

C#, Java, etc. Clojure
Direct references to things that can change Indirect references to thing that never change
Concurrency is all convention, all manual Concurrency semantics are built in
##Review - Lisp (List Processing) - Structural Sharing - Concurrency differences
##References - Code is Data: [https://dzone.com/articles/code-data-data-code#!](https://dzone.com/articles/code-data-data-code#!) - Macros: [https://dzone.com/articles/macro-lifecycle-clojure#!](https://dzone.com/articles/macro-lifecycle-clojure#!) - [https://en.wikipedia.org/wiki/Homoiconicity](https://en.wikipedia.org/wiki/Homoiconicity) - Presentation: [https://github.com/hakimel/reveal.js](https://github.com/hakimel/reveal.js) - Structural Sharing: [https://www.youtube.com/watch?v=wASCH_gPnDw](https://www.youtube.com/watch?v=wASCH_gPnDw) - Concurrency: [https://www.youtube.com/watch?v=dGVqrGmwOAw](https://www.youtube.com/watch?v=dGVqrGmwOAw)