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.

Why we love Clojure? GeekOut 2018 presentation

884 views

Published on

How we learned to love functional programming and let Clojure into our hearts. How Clojure has worked for Solita in our projects? What's so special about it? A presentation from GeekOut 2018.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Why we love Clojure? GeekOut 2018 presentation

  1. 1. Clojure How we learned to love functional programming and let Clojure into our hearts
  2. 2. Source: https://xkcd.com/297/
  3. 3. Poll time
  4. 4. About us TATU TARVAINEN Software Architect, Solita Parenthusiast, full-stack Clojure developer @tatut github.com/tatut ANTTI VIRTANEN Software Architect, Solita Security consultant, Clojure programmer @Anakondantti github.com/lokori
  5. 5. Solita skills • Lots of JVM stuff, Java most used • Analytics, Machine Learning, Cloud • Everything from eCommerce and mobile apps to systems that run trains and ice-breakers • ”If our stuff breaks, the press will publish news in 15 minutes.” Data Security Code Design
  6. 6. Solita locations FINLAND • Helsinki • Oulu • Tampere SWEDEN • Stockholm ESTONIA • Tallinn
  7. 7. Tallinn office
  8. 8. About Clojure • Practical Lisp for the JVM • Also ClojureScript (compile-to-JS) and ClojureCLR (.NET) • Functional programming • Stable • 1.0 released in 2009
  9. 9. Clojure: Functional [1 2 3 4 5 6 7 8] (2 4 6 8) (filter even? numbers)
  10. 10. Clojure: Functional [11 100 555] 666 (reduce + numbers)
  11. 11. Clojure: Functional [“Hello” “Tere” “Moi”] (“Hello World” “Tere Tallinn” “Moi maailma”) (map str greetings places) [“World” “Tallinn” “maailma”] Core function to concatenate strings
  12. 12. Clojure: Functional • Immutable data! • You only need a few types of data • Rich library of functions to work with it “It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.” - Alan Perlis
  13. 13. Clojure: Functional AVOID: Mutable shared state AVOID: Side effects AVOID: Magic setName(“Jack”) database file person function
  14. 14. Clojure: Practical • Clojure is also practical and powerful • Real Work ™ vs academic PL research • Compatibility is taken seriously
  15. 15. Clojure: Practical • Clojure implements relevant Java collection API interfaces • Java collections can be used with Clojure functions
  16. 16. Clojure: Lisp THE LISP DEVELOPER EXPERIENCE • REPL based development • Redefine while running • No JVM agents • Homoiconicity and powerful macros • Code = data • Macros can generate any code
  17. 17. Clojure: Lisp clojure.lang.PersistentList
  18. 18. Clojure: Lisp clojure.lang.PersistentVector
  19. 19. Clojure: Lisp clojure.lang.Symbol
  20. 20. Clojure: Examples MAPS (DICTIONARIES) ARE ALSO FUNCTIONS OF THEIR KEYS
  21. 21. Domain Modeling
  22. 22. Domain modeling • How Clojure and Java differ in “traditional” information systems? • Example: orders, order items etc • Object-oriented way: model each entity as a Java class • Class mirrors db table (POJO + DAO) • Old EJB-style: mix data and behaviour
  23. 23. Domain modeling: Java
  24. 24. Domain modeling: Clojure • Information ≠ class • primitives, maps, sets, vectors and lists • Just use data • Order is a map with well known keys • Database access with functions • (Optionally) clojure.spec definitions for “typing”
  25. 25. Domain modeling: Clojure
  26. 26. Domain modeling: Clojure
  27. 27. Domain modeling: Clojure • Information is easy • Powerful standard library • Missing information not a problem • No explosion of classes for different uses • Data separated from behaviour! • No LazyInitializationException {:order/customer-id 1 :order/items ...items...} {:order/state :new :order/date (time/now)} {:order/state :new :order/date (time/now) :order/customer-id 1 :order/items ...items...} (merge defaults order) (insert! db order)
  28. 28. Domain modeling: Clojure • What about data types? • Use clojure.spec to define • Map keys • Predicates values must satisfy • (shameless plug) use specql (https://github.com/tatut/specql) to automatically generate specs from PostgreSQL
  29. 29. How it all began
  30. 30. Dear Java, I’ve been dating others • Solita was a technology driven Java house from 1996 to 2007... • Microsoft-technologies came... • And in 2012 Clojure... • Why Clojure? Scala was all the rage!
  31. 31. 2010-2012: Hide the pain • We felt Java was not perfect. • We had evaluated and tested Scala. • Clojure was completely different. Pretty much no one was using it. • Took a calculated risk! It was a JVM language after all.
  32. 32. 2012: The first steps • Publicly funded Lupapiste project for construction permit applications. • Two architecture proposals • Clojure + MongoDB (server side HTML + KnockoutJS, ClojureScript not mature yet) • Java + SQL database
  33. 33. 2013: Steadily walking • Before finishing the first project, we started another one. • Bought a little external training. • ClojureScript still not mature enough. Clojure attracted some very good programmers!
  34. 34. 2015: Running at full speed • Clojure had become one of our core technologies. • Important for the success: 1. Developer support & interest. 2. Extensive Java competence. 3. Choosing our battles.
  35. 35. 2018: Clojure now • About 40 Clojure programmers (out of 300 or so) • Many projects, some of them quite big (10+ person years) • Our competitors have also started using Clojure. • Many customers are fine with Clojure, no longer a “threat”.
  36. 36. Modern Java vs. Clojure Would we do it again?
  37. 37. LISP features become mainstream Many LISP features are being adopted by Java (and others). Java 6 vs. Clojure is different than Java 10 vs. Clojure! Not everything. Not seeing anything like LISP macros for example.
  38. 38. Lambda the ultimate! Java 1.0 had anonymous inner classes. Wasn’t cool. Cooler lambdas are now mainstream. 1959 Lisp 1994 Python FP 2004 Scala 2009 Clojure 1.0 2011 C++ 2012 Kotlin 2014 Java 8
  39. 39. Try-with-resources
  40. 40. What if.. You had Lisp macros… Y
  41. 41. Java, I dare you! (C++, good luck with your template meta-programming) A parameter, so expr is data Now wait, expr is code!
  42. 42. Homoiconocity == Reflection on steroids Code is data. Data is code.
  43. 43. ??????? ??????? ???????
  44. 44. Using macros Macro takes code and makes arbitrary transformation… Before the compiler sees that code!
  45. 45. Arbitrary code transformation means Lisp compilation..
  46. 46. … is Turing complete!
  47. 47. Demo
  48. 48. ClojureScript • ClojureScript is Clojure that targets JavaScript runtimes • Compiles Clojure code to JS (with Google Closure) • Use the full power of Clojure in the browser (including macros) • Mature, stable and actively developed • Announced July 2011 • Latest release (1.10.238) in march
  49. 49. ClojureScript • Reagent • Easy access to Facebook’s React and 3rd party components • Purely functional UI (data->DOM) • “HTML as Clojure data” + =
  50. 50. ClojureScript • Figwheel: best in class live reload and interactive development for the browser! • Press Save in editor and instantly see changes in the browser • Reloads CSS • REPL • Write Clojure code which is compiled on the fly and sent to the browser for evaluation • Interact with a running SPA in the browser
  51. 51. ClojureScript • Synergy! • Use Clojure in the backend and the frontend • Communicate in Clojure data (richer than JSON) • Share common logic code easily • Validations • Clojure.spec predicates
  52. 52. http://webjure.org/mato/
  53. 53. Conclusions
  54. 54. Invest in people
  55. 55. Native REPL-workflow
  56. 56. Focus on information
  57. 57. Superpowers built-in
  58. 58. Thank You! Antti Virtanen & Tatu Tarvainen Solita
  59. 59. Where to start • Clojure.org (official site) • Clojure Google Group • Clojurians Slack • /r/Clojure • https://clojutre.org/2018/
  60. 60. Follow us solita.fi/en/ solita.fi/en/blogs dev.solita.fi Twitter Facebook LinkedIn YouTube Instagram

×