Clojure

1,322 views

Published on

A survey build around Stuart Halloway's "4 pitches" for Clojure. Presented by Alan Dipert and Justin Kramer at BarCamp Rochester 2010

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,322
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
16
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Give credit to halloway
  • Explain REPL Talke about write/compile/test cycle and experiental programming
  • Clojure

    1. 1. Clojure A Whirlwind Tour Alan Dipert, @alandipert Justin Kramer, @jkkramer
    2. 2. Clojure is... <ul><li>A Lisp dialect
    3. 3. Functional
    4. 4. Tightly integrated with Java/C#
    5. 5. Dynamic
    6. 6. Built for concurrency
    7. 7. Two years old, at version 1.1 </li></ul>
    8. 8. Clojure's 4 Pitches <ul><li>Lisp
    9. 9. Functional programming
    10. 10. Java/C# interoperability
    11. 11. Language-level state support </li></ul>
    12. 12. Why Lisp? <ul><li>Little syntax to learn
    13. 13. Say a lot with a little. Less code, fewer bugs
    14. 14. Flexible, it becomes what you want
    15. 15. Code is data, “homoiconic” </li></ul>
    16. 17. Clojure Data Simple Data Compound Data a “ house” 42 [1 2 3 4] true (true false true) :fname {:fname “Alan” :lname “Dipert} 7/8 #{“bar” “camp” “rules”}
    17. 18. Expressions 34 34 (+ 1 2) 1 + 2 (println (str “The date is ” (new Date))) println(str(“The date is”, new Date)) (def X 7) X = 7 (< 7 8 9) (7 < 8) && (8 < 9) (if (< X 8) “less than 8” “at least 8”) X < 8 ? “less than 8” : “at least 8”
    18. 19. Evaluation and the REPL <ul><li>Enter one command at a time
    19. 20. Experimental programming and one-offs </li></ul>
    20. 21. Functional Programming <ul><li>A programming paradigm, set of language features
    21. 22. Functions can be created dynamically
    22. 23. Functions can be stored just like other values, are “first class”
    23. 24. Functions can be passed to functions
    24. 25. Values are immutable </li></ul>
    25. 26. Why Functional Programming? <ul><li>Functions are dependable building blocks </li><ul><li>Especially when they're “side-effect free” </li></ul><li>Function composition is a powerful modularization technique
    26. 27. Immutable values combined with function composition make concurrency an easier nut to crack </li></ul>
    27. 29. Higher Order Functions A higher order function is a function that takes one or more other functions as arguments. map, select, filter, reject, reduce, foldl, inject, fold...
    28. 30. Higher Order Functions in Action (map (fn [x] (+ x 1)) [1 2 3 4]) ;; (map inc ...) [2 3 4 5] (filter odd? (range 10)) (1 3 5 7 9) (every? odd? [1 3 5 7 9]) true (reduce + [1 2 3]) 6 (drop-while even? [2 4 8 9 13 43]) (9 13 43) (take-while #(< % 10) [1 3 11 42 102]) (1 3)
    29. 31. Java/C# Interoperability <ul><li>Clojure runs on both the JVM and CLR </li><ul><li>It's easy to create, call, and interact with objects (arguably easier than it is in Java itself!) </li></ul><li>Clojure lets you: </li><ul><li>Call static methods
    30. 32. Create classes that extend others, or implement interfaces
    31. 33. Instantiate object instances
    32. 34. Do lots of other, really cool things </li></ul></ul>
    33. 35. Java/C# Interoperability <ul><li>With the power of Lisp, you can cleanly abstract away common OOP patterns using Lisp's macro system
    34. 36. You can program functionally and still take advantage of the Java ecosystem's huge library base
    35. 37. You can write homework assignments in Clojure and hand your professor a .jar </li></ul>
    36. 38. Java/C# Interoperability (every? #(Character/isWhitespace) “hallo”) ;; false (every? #(Character/isWhitespace) “ “) ;; true (. javax.swing.JOptionPane (showMessageDialog nil &quot;Sup!&quot;))
    37. 39. State <ul><li>The problem of concurrency is really one of state .
    38. 40. Mutable state sucks for sharing, i.e. for concurrency
    39. 41. Managing state in OOP is too complex </li><ul><li>“Stop the world while I look at it” </li></ul><li>We need reliable building blocks </li></ul>
    40. 43. State <ul><li>Clojure gives us better tools: </li><ul><li>Pure Functions
    41. 44. Immutability (with Persistent Data Structures)
    42. 45. Managed state transition </li><ul><li>Value, identity, state </li></ul></ul></ul>
    43. 46. Persistent Data Structures <ul><li>Immutability is traditionally expensive </li><ul><li>One way is to make copies of data structures, which is slow.
    44. 47. Clojure's way is to use red/black trees for complex data. Not constant time, but log32 n. Good enough. </li></ul></ul>
    45. 48. Persistent Data Structures
    46. 49. State <ul><li>Snapshots vs. “Stop the World”
    47. 50. Identities = references </li><ul><li>Vars
    48. 51. Refs (STM – transactions) </li><ul><li>Bank account transfer </li></ul><li>Agents
    49. 52. Atoms </li></ul><li>Drawback: slower :( </li></ul>
    50. 53. Questions? Thanks! http://clojure.org

    ×