Clojure Small Intro

of 49 /49
Clojure John Vlachoyiannis @jonromero [email protected] Http://jon.is.emotionull.com

Embed Size (px)

description

Presentation for JHUG in Greece

Transcript of Clojure Small Intro

  • 1. Clojure John Vlachoyiannis @jonromero [email protected]://jon.is.emotionull.com
  • 2. What is Clojure?
  • 3. Ok, what is Lisp?
  • 4. Lisp is worth learning for the profound enlightenment experience you will havewhen you finally get it; that experience willmake you a better programmer for the restof your days, even if you never actually use Lisp itself a lot." Eric S. Raymond, "How to Become a Hacker".
  • 5. LISP stands for: Lots of Insane Stupid Parentheses Anonymous
  • 6. The Truth about Lisp
  • 7. LISt Processing LIS Second oldest high-level language (first is Fortran) Code as Data (Homoiconic) Perfect for Domain-specific languages (DSL) Exploratory programming
  • 8. Clojure Lisp in JVM Concurrent programming Dynamic Development (REPL) Lazy sequences No side effects (almost)
  • 9. Enter Clojure
  • 10. Everything is code
  • 11. (println "Hello World")function argument
  • 12. Everything is data
  • 13. (println "Hello World")list symbol string
  • 14. Integers 1234567891234 Doubles 3.14. BigDecimals 3.14M Ratios 22/4 Strings foo, Character a b c Symbols foo, Keywords :foo Booleans true false, Null nil Regex patterns #[a-zA-Z0-9]|
  • 15. Data structures Lists (1 2 3 4), (foo bar baz), (list 1 2 3) Vectors [1 2 3], [foo bar], (vector 1 2 3) Maps {:a 1 :b 2 :c 3} Sets #{foo bar}
  • 16. It is better to have 100functions operate on one data structure than to have 10 functions operate on 10 data Structures. Alan J. Perlis
  • 17. clojure might be a better java than java
  • 18. public class StringUtils { public static boolean isBlank(String str) { int strLen; if (str == null || (strLen = str.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) ==false)) { return false; } } return true; }}
  • 19. public class StringUtils { public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } for (i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) ==false)) { return false; } } return true; }}
  • 20. public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } for (i = 0; i < strLen; i++) { if ((Character.isWhitespace(str.charAt(i)) == false)) return false; } } return true;}
  • 21. public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } every (ch in str) { Character.isWhitespace(ch); } return true;}
  • 22. public isBlank(str) { every (ch in str) { Character.isWhitespace(ch); }}
  • 23. (defn blank? [s] (every? #(Character/isWhitespace %) s))
  • 24. Clojure vs Java code Side-effect free Error prone Easy to (unit) test Not so easy Lazy collection Only one element Any element Only with chars Slower Faster Data manipulation If/for/while code Exploratory Design Is Law
  • 25. Clojure is a functional language Functions are first-class objects Data is immutable Functions are pure
  • 26. So what?
  • 27. Simple: no loops, variables or mutable state Thread-safe: no locking Parallelizable: map/reduce anyone? Generic: data is always data Easy to test: same input, same output
  • 28. user=> (println "hello world")| hello world-> nil
  • 29. user=> (defn hello [name] (str "Hello, " name))#user/hello
  • 30. (hello "Clojure")
  • 31. (.toUpperCase hello)
  • 32. (str hello world)
  • 33. (+ 1 3 4 (* 5 6))
  • 34. (defn greeting"Returns a greeting of the form Hello, username."[username](str "Hello, " username))
  • 35. (greeting "world")
  • 36. user=> (doc greeting) ------------------------- exploring/greeting ([username])Returns a greeting of the form Hello, username.
  • 37. (defn is-small? [number] (if (< number 100) "yes" ))
  • 38. (is-small? 50) "yes"
  • 39. (is-small? 50000) nil
  • 40. (defn is-small? [number] (if (< number 100) "yes" "no" ))
  • 41. Solving problems Experiment with the problem Create your data structures Data transformations Write code that writes code for you (macros) Create a mini language (a DSL)
  • 42. Java in Clojure Yeap
  • 43. Clojure in Java Yeap
  • 44. Clojure in Clojure Yeap, yeap
  • 45. Java in Clojurejava new Widget(foo)clojure (Widget. foo)java dialog.show()clojure (.show dialog)
  • 46. Tools Emacs + SLIME ViMClojure Enclojure + Intellij IDEA Counterclockwise + Eclipse Leiningen
  • 47. Clojure is not a just a new language
  • 48. Is another, simplier way to solve problems
  • 49. Thanks! Questions? @jonromero