Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Web Apps with Clojure

Building Web Apps with Clojure

An introduction to Clojure and a survey of the existing tools for using the language to build web apps.

sbecker

July 03, 2012
Tweet

Other Decks in Programming

Transcript

  1. "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
  2. "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
  3. • 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
  4. Infix Notation Prefix Notation 3 + 4 + 3 4

    (5 - 6) * 7 * (- 5 6) 7 1 + 2 + 3 + 4 + 1 2 3 4 Tuesday, July 3, 12
  5. C / Ruby / JS Lisp S-Expressions 3 + 4

    (+ 3 4) (5 - 6) * 7 (* (- 5 6) 7) 1 + 2 + 3 + 4 (+ 1 2 3 4) Tuesday, July 3, 12
  6. Lisp S-Expressions => (+) 0 => (+ 2) 2 =>

    (+ 2 3) 5 => (+ 2 3 4) 9 => (+ 2 3 4 5) 14 Tuesday, July 3, 12
  7. JavaScript Functions Clojure Functions function sayHello(name) { console.log("Hello, " +

    name + "!"); } sayHello("Portland"); // Hello, Portland! doSomethingWith(value1, value2); (defn say-hello [name] (println (str "Hello, " name "!"))) (say-hello "Portland") ; Hello, Portland! (do-something-with value1 value2) Tuesday, July 3, 12
  8. 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
  9. “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
  10. • modify passed-in arguments • change some internal or external

    state • pass same arguments and yield different results Tuesday, July 3, 12
  11. Philosophy “most parts of most programs should be functional, programs

    that are more functional are more robust.” Tuesday, July 3, 12
  12. 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
  13. 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
  14. 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
  15. 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
  16. Sets • Collections of unique values • Basic set operations

    like union / difference /intersection #{:a :b :c :d} Tuesday, July 3, 12
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. Barriers • Initial learning curve • Prefix Notation, S-Expressions •

    Functional vs Object Oriented • Immutable data • Recursion vs looping Tuesday, July 3, 12
  24. Build Something Real • Something practical • Learn by doing

    • Crawl before you walk • Canonical web examples: todo lists & blogs Tuesday, July 3, 12
  25. • 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
  26. Response Map {:status <Integer> :headers <IPersistentMap> :body <String, ISeq, File,

    InputStream>} {:status 200 :headers {“Content-Type”: “text/html”} :body “Hello Portland”} Tuesday, July 3, 12
  27. 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