Building Web Apps with Clojure
Tuesday, July 3, 12
Slide 2
Slide 2 text
Why Clojure?
Tuesday, July 3, 12
Slide 3
Slide 3 text
Based on Lisp
Tuesday, July 3, 12
Slide 4
Slide 4 text
"the greatest single programming language ever designed"
- Alan Kay, on Lisp
Tuesday, July 3, 12
Slide 5
Slide 5 text
"Lisp is worth learning for the profound enlightenment experience you will have
when you finally get it; that experience will make you a better programmer for the
rest of your days, even if you never actually use Lisp itself a lot."
- Eric Raymond, "How to Become a Hacker"
Tuesday, July 3, 12
Slide 6
Slide 6 text
"Lisp has jokingly been called "the most intelligent way to misuse a computer". I
think that description is a great compliment because it transmits the full flavor of
liberation: it has assisted a number of our most gifted fellow humans in thinking
previously impossible thoughts."
- Edsger Dijkstra
Tuesday, July 3, 12
Slide 7
Slide 7 text
• Originated in 1958. Second only to Fortran.
• The name LISP derives from LISt Processing.
• Lisp source code is made up of lists.
• Homoiconic: Code is data. Data is code.
(def lisp “Your grandpa’s language”)
Tuesday, July 3, 12
First Class Functions
• pass functions as arguments
• higher order functions - return functions as values
• assign functions to variables
• store functions in data structures
Tuesday, July 3, 12
Slide 15
Slide 15 text
Pure Functions
• Same input always returns same output
• No side effects
Tuesday, July 3, 12
Slide 16
Slide 16 text
“A function is said to have a side effect if, in addition to returning a
value, it also modifies some state or has an observable interaction with
calling functions or the outside world.”
Side Effects
Tuesday, July 3, 12
Slide 17
Slide 17 text
• modify passed-in arguments
• change some internal or external state
• pass same arguments and yield different results
Tuesday, July 3, 12
Slide 18
Slide 18 text
Clojure is impure
Clojure is Impure
Tuesday, July 3, 12
Slide 19
Slide 19 text
Philosophy
“most parts of most programs should be functional, programs that are more
functional are more robust.”
Tuesday, July 3, 12
Slide 20
Slide 20 text
Immutable Data Structures
lists, vectors, maps, sets
Tuesday, July 3, 12
Slide 21
Slide 21 text
Primitives
Tuesday, July 3, 12
Slide 22
Slide 22 text
Primitives
• Numbers, Booleans, Nil, Strings
• Symbols - stand-in names for values, like constants
• Keywords - names not bound to a value
these
are
symbols
:these
:are
:keywords
Tuesday, July 3, 12
Slide 23
Slide 23 text
Lists
• Like arrays, but evaluated as a function (by default)
• First item is treated as a function name
• Remaining items are the arguments
• Can also be treated as data
=> (+ 1 2 3)
6
=> ‘(+ 1 2 3)
(+ 1 2 3)
Tuesday, July 3, 12
Slide 24
Slide 24 text
Vectors
• Zero-based arrays
• Can contain any value, and any mix of value types
• Not executed as code
=> [1 2 3]
[1 2 3]
=> [:a 0 “hello”]
[:a 0 “hello”]
Tuesday, July 3, 12
Slide 25
Slide 25 text
Maps
• Define a set of unique key-value pairs
• Comma separation optional for readability, treated the same
as whitespace
{:name “Clojure”
:functional true
:version “1.4.0”}
Tuesday, July 3, 12
Slide 26
Slide 26 text
Sets
• Collections of unique values
• Basic set operations like union / difference /intersection
#{:a :b :c :d}
Tuesday, July 3, 12
Map
; increment each number
=> (map inc [1 2 3])
(2 3 4)
; anonymous function, add 10
=> (map #(+ 10 %) [1 2 3])
(11 12 13)
Applies a function to each element in a collection
and returns a new collection
Tuesday, July 3, 12
Slide 29
Slide 29 text
Reduce
=> (reduce + [2 3 4])
9
=> (reduce * [2 3 4])
24
Applies a function to all elements in a collection
and returns a value or collection
Tuesday, July 3, 12
Slide 30
Slide 30 text
Filter
=> (filter even? [1 2 3 4 5 6])
(2 4 6)
Applies a predicate function to each element in
a collection and returns a new filtered collection
Tuesday, July 3, 12
Slide 31
Slide 31 text
Hosted on the JVM
Tuesday, July 3, 12
Slide 32
Slide 32 text
Host Platforms: JVM, JS, .NET
• Clojure runs on the Java Virtual Machine.
• at near native Java speed
• with access to any library on the JVM
• Also runs on JavaScript, and .NET
Tuesday, July 3, 12
Slide 33
Slide 33 text
Built for Concurrency
Tuesday, July 3, 12
Slide 34
Slide 34 text
Simple Made Easy
http://www.infoq.com/presentations/Simple-Made-Easy-QCon-London-2012
Tuesday, July 3, 12
Slide 35
Slide 35 text
Simple Made Easy
• Emphasizing ease gives early speed
• Ignoring complexity will slow you down long term
• Many tools that are easy to use yield complex results
• Complect: to intertwine, entwine, or braid things
• Compose: to place together
• Composing simple components - the key to robust software
Tuesday, July 3, 12
Slide 36
Slide 36 text
State is never simple
• Complects value and time
• It is easy - in the sense that its at hand and familiar
• Interweaves everything that touches it
Tuesday, July 3, 12
Slide 37
Slide 37 text
Barriers
Tuesday, July 3, 12
Slide 38
Slide 38 text
Barriers
• Initial learning curve
• Prefix Notation, S-Expressions
• Functional vs Object Oriented
• Immutable data
• Recursion vs looping
Tuesday, July 3, 12
Slide 39
Slide 39 text
Build Something Real
• Something practical
• Learn by doing
• Crawl before you walk
• Canonical web examples: todo lists & blogs
Tuesday, July 3, 12
Slide 40
Slide 40 text
Editors
Tuesday, July 3, 12
Slide 41
Slide 41 text
Tuesday, July 3, 12
Slide 42
Slide 42 text
Leiningen
The Clojure Project Automation tool
Tuesday, July 3, 12
Slide 43
Slide 43 text
Dependency Management
Tuesday, July 3, 12
Slide 44
Slide 44 text
Tuesday, July 3, 12
Slide 45
Slide 45 text
Ring
Tuesday, July 3, 12
Slide 46
Slide 46 text
• Abstracts HTTP
• Similar to Ruby's Rack and Python's WSGI
• Handlers are functions that take requests at Clojure Maps
• and return responses as a Clojure Map
• Adapters run handlers on a web server
• Middleware augment handlers
Ring
Tuesday, July 3, 12
FIN
https://github.com/sbecker/osbridge-clojure-blog
Source Code
http://osbridge-clojure-blog.herokuapp.com
Demo Site
Tuesday, July 3, 12
Slide 66
Slide 66 text
Photo Credits
Lego Store - http://www.flickr.com/photos/vpickering/6297481250/
Clojure Logo - http://diego-pacheco.blogspot.com/2012/01/clojure-hell-yeah.html
Lisp on Paper - http://www.flickr.com/photos/phil-jackson/3044578328/
Functional Bag of Goodies - http://www.flickr.com/photos/beorn_ours/5675267679/
Side Effect Pedals - http://www.flickr.com/photos/terekhova/4629820574/
Dye Water Glasses - http://www.flickr.com/photos/pyth0ns/4816846174/
Galaxy - http://www.flickr.com/photos/skiwalker79/3855880846/
Series of Bullets - http://www.flickr.com/photos/dvids/7204296286/
Coffee Beans - http://www.flickr.com/photos/kubina/1469914113/sizes/l/in/photostream/
Bird Formation - http://www.flickr.com/photos/deapeajay/2694162918/
Great Wall - http://www.flickr.com/photos/franck-chilli/3882326984
Tuesday, July 3, 12