Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Atmosphere 2016 - Krzysztof Kaczmarek - Don't fear the brackets - Clojure in the eyes of Java Developer

74 views

Published on

Find out why Clojure delighted Uncle Bob and why is used by huge corporations like Netflix, Wallmart, Daily Mail, (Allegro is joining this list) and why, regarding Greenspun's tenth rule, you already wrote your software in lisp.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Atmosphere 2016 - Krzysztof Kaczmarek - Don't fear the brackets - Clojure in the eyes of Java Developer

  1. 1. Clojure
  2. 2. About me • humble software engineer @allegro • kung fu master in spare time • Almost none of those thoughts are mine. Presumably there are no new thoughts after Aristotle. For sure after 1970…
  3. 3. What we think we are doing? Computer science
  4. 4. What do we do?
  5. 5. What do we really do?
  6. 6. What we really do
  7. 7. Meet Rich
  8. 8. Who • Object Oriented? • Functional Programming? • What does it mean?
  9. 9. stolen 
 from Uncle
 Bob
  10. 10. Async is everywhere
  11. 11. OO vs functional
  12. 12. Quiz )}; )}; )}; )}; )}; )}; Most frequent code in JS Most frequent code in Clojure ) ) ) ) ) ) ) )
  13. 13. First impressions
  14. 14. Quick intro to clojure
  15. 15. John McCarty • Lisp • interpreters • high-level metaprogramming • garbage collection • dynamic programming • functional programming • programming with recursive functions • …
  16. 16. Quick intro to clojure • f(x,y) -> (f x y) • deleteOffer(2, :force) -> (delete-offer 2 :force) function params
  17. 17. (def x 1) int x = 1; (fn [x] (* x x)(def f )) (f 5) (defn f [x] (* x x))
  18. 18. Quick intro to clojure • List
 (1 2 3 4 5) (adrian krzychu wojtek) (list 1 2 3) • Vectors
 [1 2 3 4 5] [fred ethel lucy] • Map 
 {:a 1, :b 2, :c 3} {1 “ethel” 2 “fred} • Sets
 #{fred ethel lucy} • Everything nested

  19. 19. Persistent data structure
  20. 20. Tail recursion
  21. 21. show some code!
  22. 22. (defn neighbours [[x y]] (for [dx [-1 0 1] dy (if (zero? dx) [-1 1] [-1 0 1])] [(+ dx x) (+ dy y)])) (defn step [cells] (set (for [[loc n] (frequencies (mapcat neighbours cells)) :when (or (= n 3) (and (= n 2) (cells loc)))] loc))) code from Christophe Grand
  23. 23. stolen from Rich Hickey
  24. 24. stolen from Rich Hickey
  25. 25. stolen from Rich Hickey
  26. 26. stolen from Rich Hickey
  27. 27. The same? Json def builder = new groovy.json.JsonBuilder() def root = builder.people { person { firstName 'Krzysztof' lastName 'Kaczmarek' } } (json {:person { :firstName “Krzysztof” :lastName “Kaczmarek”}}) Groovy Clojure
  28. 28. The same? Config offerCoreStockService: serviceAddress: service://offercore-stock-service offerCoreService: serviceAddress: service://offercore-service-server {:offerCoreStockService :serviceAddress “service://offercore-stock-service” :offerCoreService :serviceAddress “service://offercore-service-server”} YML Clojure (edn)
  29. 29. The same?
  30. 30. – Greenspun's tenth rule “Any sufficiently complicated C or Fortran program contains an ad hoc, informally- specified, bug-ridden, slow implementation of half of Common Lisp.”
  31. 31. @RestController @EnableAutoConfiguration 
 public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); 
 @RequestMapping("/greeting") public Greeting greeting( @RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.getAndIncrement(), } } String.format(template, name)); example provided by P. Kapała
  32. 32. @RestController @EnableAutoConfiguration 
 public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); 
 @RequestMapping("/greeting") public Greeting greeting( @RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.getAndIncrement(), } } String.format(template, name)); example provided by P. Kapała
  33. 33. (def counter (atom 0)) (defn greet [target] 
 {:id (swap! counter inc) :name (str "Hello, " (or target "World") "!")}) (defroutes app-routes 
 (GET "/greeting" {params :params} (response (greet (:name params)))) (route/not-found "Not Found")) example provided by P. Kapała
  34. 34. (def counter (atom 0)) (defn greet [target] 
 {:id (swap! counter inc) :name (str "Hello, " (or target "World") "!")}) (defroutes app-routes 
 (GET "/greeting" {params :params} (response (greet (:name params)))) (route/not-found "Not Found")) example provided by P. Kapała
  35. 35. –-- Dr. Alan Kay, 2003. “OOP to me means only messaging, local retention and protection and hiding of state- process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I'm not aware of them.”
  36. 36. –Rich Hickey “Not everything is awesome.”
  37. 37. Fear - dynamic typing Report createReport(Data data) {…} vs (create-report data)
  38. 38. Fear: Refactoring
  39. 39. Clojure w allegro
  40. 40. Success story? • Simple rest service • 2000 lines of code in java • 150 lines of code in clojure (one 27” screen) • most people exp: 1/5 of codebase
  41. 41. Java interop (. Array new) (. MyAwesomeService doJava “foo” “bar”)
  42. 42. Adoption
  43. 43. Last language?
  44. 44. ClojureScript • Hosted • Hello world => 2 LOC in JS • Is it slow? • Debugging • > 2300 watchers • 68 contributors
  45. 45. Clojure.async • queues in app • making app simpler to reason about
  46. 46. Clojure.async First go block >! my queue <! second go block
  47. 47. How to learn
  48. 48. 4clojure.org
  49. 49. Hype is coming
  50. 50. –Adrian Cockroft “A lot of the best programmers and the most productive programmers I know are writing everything in Clojure and swearing by it, and then just producing ridiculously sophisticated things in a very short time. And that programmer productivity matters. “ http://thenewstack.io/the-new-stack-makers-adrian-cockcroft-on-sun-netflix-clojure- go-docker-and-more/
  51. 51. Q & A? krzysztof.kaczmarek@me.com

×