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.

981b75f949c81efad3bc4b3c4ea88332?s=128

sbecker

July 03, 2012
Tweet

Transcript

  1. Building Web Apps with Clojure Tuesday, July 3, 12

  2. Why Clojure? Tuesday, July 3, 12

  3. Based on Lisp Tuesday, July 3, 12

  4. "the greatest single programming language ever designed" - Alan Kay,

    on Lisp Tuesday, July 3, 12
  5. "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
  6. "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
  7. • 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
  8. 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
  9. 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
  10. Lisp S-Expressions => (+) 0 => (+ 2) 2 =>

    (+ 2 3) 5 => (+ 2 3 4) 9 => (+ 2 3 4 5) 14 Tuesday, July 3, 12
  11. 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
  12. REPL Tuesday, July 3, 12

  13. Functional Functional Tuesday, July 3, 12

  14. 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
  15. Pure Functions • Same input always returns same output •

    No side effects Tuesday, July 3, 12
  16. “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
  17. • modify passed-in arguments • change some internal or external

    state • pass same arguments and yield different results Tuesday, July 3, 12
  18. Clojure is impure Clojure is Impure Tuesday, July 3, 12

  19. Philosophy “most parts of most programs should be functional, programs

    that are more functional are more robust.” Tuesday, July 3, 12
  20. Immutable Data Structures lists, vectors, maps, sets Tuesday, July 3,

    12
  21. Primitives Tuesday, July 3, 12

  22. 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
  23. 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
  24. 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
  25. 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
  26. Sets • Collections of unique values • Basic set operations

    like union / difference /intersection #{:a :b :c :d} Tuesday, July 3, 12
  27. Iterators each, map, filter, reduce, recur map, filter, reduce, doseq,

    recur Iterators Tuesday, July 3, 12
  28. 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
  29. 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
  30. 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
  31. Hosted on the JVM Tuesday, July 3, 12

  32. 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
  33. Built for Concurrency Tuesday, July 3, 12

  34. Simple Made Easy http://www.infoq.com/presentations/Simple-Made-Easy-QCon-London-2012 Tuesday, July 3, 12

  35. 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
  36. 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
  37. Barriers Tuesday, July 3, 12

  38. Barriers • Initial learning curve • Prefix Notation, S-Expressions •

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

    • Crawl before you walk • Canonical web examples: todo lists & blogs Tuesday, July 3, 12
  40. Editors Tuesday, July 3, 12

  41. Tuesday, July 3, 12

  42. Leiningen The Clojure Project Automation tool Tuesday, July 3, 12

  43. Dependency Management Tuesday, July 3, 12

  44. Tuesday, July 3, 12

  45. Ring Tuesday, July 3, 12

  46. • 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
  47. Request Map {:uri <String> :query-string <String> :request-method <Keyword> :headers <IPersistentMap>

    :body <InputStream> ... ... } Tuesday, July 3, 12
  48. 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
  49. Example Hello World Tuesday, July 3, 12

  50. Tuesday, July 3, 12

  51. Lein-Noir Tuesday, July 3, 12

  52. Noir Project Structure Tuesday, July 3, 12

  53. Noir Views Tuesday, July 3, 12

  54. Generating HTML Tuesday, July 3, 12

  55. Hiccup Tuesday, July 3, 12

  56. Rendering Posts Tuesday, July 3, 12

  57. Data Persistence Tuesday, July 3, 12

  58. Tuesday, July 3, 12

  59. Simple SQL DSL Tuesday, July 3, 12

  60. It’s Just Data Tuesday, July 3, 12

  61. Testing Tuesday, July 3, 12

  62. Midje - Fact Checking Tuesday, July 3, 12

  63. Deployment Tuesday, July 3, 12

  64. Heroku $ heroku create osbridge-clojure-blog $ heroku addons:add shared-database $

    git push heroku master Tuesday, July 3, 12
  65. FIN https://github.com/sbecker/osbridge-clojure-blog Source Code http://osbridge-clojure-blog.herokuapp.com Demo Site Tuesday, July 3,

    12
  66. 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