Post on 06-May-2015
description
transcript
Why Clojure?
Why Clojure?
Why was it created?
What do we mean with complexity?
How does clojure makes things more simple?
When was Clojure
● 2007: introduction by Rich Hickey
● 2009: version 1.0
....
● 2013: version 1.5
What is Clojure
● A Lisp
● for Functional Programming
● Running on the JVM, CLR and browsers
● Designed for concurrency
A Lisp
Many concepts of Lisps are taken over by other languages:● Conditionals● First-class functions● Recursion● Garbage collection● Programs made up out of expressions
Functional Programming
● Provides a way out of the multi-core conundrum
● Makes programs simpler by decomplecting them.
Running on the JVM, CLR, browser
● JVM / CLR have huge benefits:○ Libraries○ Memory and resource management○ Efficient and robust
● JavaScript has a huge reach:○ It's the most widely deployed language○ Through ClojureScript, we can target everywhere
JavaScript runs.● Clojure is symbiotic with its host platform.
Designed for Concurrency
● Machines are not getting faster, they just have more cores.
● Shared access to mutable state makes multi-core programming a nightmare.
● Immutability makes many of the problems go away.
● Separating identity and state can solve all the other problems.
Why is Clojure
● Reducing complexity
● to be easy? not exactly...
What do we mean with
● Complex
● Simple
● Hard
● Easy
Complexity
To Complectbe interwoven or interconnected
Complex
Consisting of many different and connected parts.
Complexity
Complex
Simple
How I/we made things complex
● Teamwork ● Robots
○ Explore unknown maze
○ Detect barcodes --> action
○ Travel over seesaw
● How to test?○ We couldn't
● Unmanageable complexity
Complexity
Simple Easy
Complexity
HardComplex
Simple Easy
HardComplex
Simple Easy
ComplexityObjective Subjective
"Elegance and familiarity are orthogonal." — Rich Hickey
Types of Complexity
● Essential Complexity
● Accidental Complexity
Examples of complexity
● State
● Inheritance
● Objects ○ state○ identity○ value
● Memory management● Variables
Why does simple matter?
● Formal reasoning○ can be hard
● Extendability○ Composing
● Testability○ isolation
How is Clojure simple(r)
● Functional Programming○ Pure functions
● Immutable data structures
● Thinking with data
● Decomplecting state / value / identity
Immutability
● Numbers are immutable in your language
● If you use Java, Strings are immutable
● Imagine if they're not!
Immutable building blocks
● Primitives: numbers, strings
● Collections: lists, vectors, strings, maps, sets
● "Changing" a collection creates an (efficient) copy. (think: Git)
● Don't worry about state
Thinking with data
● data is simple
● Why wrap it in classes
"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." — Alan Perlis
Thinking with data
● data is simple
● Clojure map
Thinking with data
; Get the first name(get-in person [:name :first-name])
; Get all birth dates from a list of persons.(map :birthdate people)
Identity/state/value model
● Object Oriented Approach
SpaceShip
- name- x- y- velocity
ship1
Memory
● Object Oriented Approach
● Problems○ Stable value
○ history (snapshot)
○ Concurrent modification
Identity/state/value model
● Spaceship○ position
■ x,y■ Position object
○ velocity■ MovementState
Stable value
● Spaceship○ position
■ x,y■ Position object
○ velocity■ MovementState
○ ...
○ Consistent and stable object!
Stable value
● Spaceship○ position
■ x,y■ Position object
○ velocity■ MovementState
○ ...
○ Consistent and stable object! NO!
Stable value
Stable value
● Creating the object
● Not consistent
● Object Oriented Approach
● Problems○ Stable value
○ history (snapshot)
○ Concurrent modification
Identity/state/value model
Snapshot & history
● Take a picture
● Look later at it again.
● Object Oriented Approach
● Problems○ Stable value
○ history (snapshot)
○ Concurrent modification
Identity/state/value model
Concurrent modification
● Guarantee consistency?
● Stop the world!
Clojure approach
● Remove ambiguity of definitions○ value○ state○ identity
Identity/state/value model
● Value○ fixed: 42 , "answer"
● Identity○ series of different values over time
Me, a car,
● State○ Value of an identity at a certain point in time
Identity/state/value model
● OOP○ complects state and identity
● Clojure○ Separation value / state / identity
○ Perception becomes possible
○ Consistency management trivial
Clojure reference model
:name Heart Of Gold:position (0,0) :velocity 100 km/s
ship1
Value
Ref
Clojure reference model
:name Heart Of Gold:position (0,0) :velocity 100 km/s
ship1
Value
Ref
:name Heart Of Gold:position (100,100):velocity 100 km/s
Value
Clojure reference model
:name Heart Of Gold:position (0,0) :velocity 100 km/s
ship1
Value
Ref
:name Heart Of Gold:position (100,100) :velocity 100 km/s
Value
Clojure reference model
● Perception without stopping the world
● Atomic updates○ Transactions
Why Clojure?
Simplicity
Expressiveness
Concurrency
Why Clojure?Are we there yet?
http://www.infoq.com/presentations/Are-We-There-Yet-Rich-Hickey
Simple made easyhttp://www.infoq.com/presentations/Simple-Made-Easy
Out of The Tarpithttp://shaffner.us/cs/papers/tarpit.pdf
Why Clojure?
?