Clojure    John Vlachoyiannis         @jonromero   jon@emotionull.comHttp://jon.is.emotionull.com
What is Clojure?
Ok, what is Lisp?
“Lisp is worth learning for the profound  enlightenment experience you will havewhen you finally get it; that experience w...
“LISP stands for: Lots of Insane Stupid            Parentheses”             Anonymous
The Truth about Lisp
LISt Processing              LIS●   Second oldest high-level language (first is    Fortran)●   Code as Data (Homoiconic)● ...
Clojure●   Lisp in JVM●   Concurrent programming●   Dynamic Development (REPL)●   Lazy sequences●   No side effects   (alm...
Enter Clojure
Everything is code
(println "Hello World")function        argument
Everything is data
(println "Hello World")list       symbol          string
●   Integers – 1234567891234●   Doubles – 3.14. BigDecimals – 3.14M●   Ratios – 22/4●   Strings – ”foo”, Character – a b c...
Data structures●   Lists   (1 2 3 4), (foo bar baz), (list 1 2 3)    ●● Vectors   [1 2 3], [foo bar], (vector 1 2 3)    ●●...
“It is better to have 100functions operate on one data   structure than to have 10 functions operate on 10 data           ...
clojure might be a better      java than java
public class StringUtils {   public static boolean isBlank(String str) {      int strLen;      if (str == null || (strLen ...
public class StringUtils {   public isBlank(str) {      if (str == null || (strLen = str.length()) == 0) {          return...
public isBlank(str) {  if (str == null || (strLen = str.length()) == 0) {      return true;  }  for (i = 0; i < strLen; i+...
public isBlank(str) {  if (str == null || (strLen = str.length()) == 0) {      return true;  }    every (ch in str) {     ...
public isBlank(str) {  every (ch in str) {      Character.isWhitespace(ch);  }}
(defn blank? [s]  (every? #(Character/isWhitespace %) s))
Clojure vs Java code●   Side-effect free      ●   Error prone●   Easy to (unit) test   ●   Not so easy●   Lazy collection ...
Clojure is a functional language●   Functions are first-class objects●   Data is immutable●   Functions are pure
So what?
●   Simple: no loops, variables or mutable    state●   Thread-safe: no locking●   Parallelizable: map/reduce anyone?●   Ge...
user=> (println "hello world")| hello world-> nil
user=> (defn hello [name]           (str "Hello, " name))#user/hello
(hello "Clojure")
(.toUpperCase “hello”)
(str “hello” “ “ “world”)
(+ 1 3 4 (* 5 6))
(defn greeting"Returns a greeting of the form Hello, username."[username](str "Hello, " username))
(greeting "world")
user=> (doc greeting)              -------------------------               exploring/greeting                  ([username]...
(defn is-small? [number]   (if (< number 100)          "yes" ))
(is-small? 50)     "yes"
(is-small? 50000)       nil
(defn is-small? [number]   (if (< number 100)           "yes"           "no" ))
Solving problems●   Experiment with the problem●   Create your data structures●   Data transformations●   Write code that ...
Java in Clojure●   Yeap
Clojure in Java●   Yeap
Clojure in Clojure●   Yeap, yeap
Java in Clojurejava           new Widget(“foo”)clojure        (Widget. “foo”)java           dialog.show()clojure        (....
Tools●   Emacs + SLIME●   ViMClojure●   Enclojure + Intellij IDEA●   Counterclockwise + Eclipse●   Leiningen
Clojure is not a just a new language
Is another, simplier way to solve problems
Thanks! Questions?   @jonromero
Upcoming SlideShare
Loading in...5
×

Clojure Small Intro

1,385

Published on

Presentation for JHUG in Greece

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

No Downloads
Views
Total Views
1,385
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Clojure Small Intro"

  1. 1. Clojure John Vlachoyiannis @jonromero jon@emotionull.comHttp://jon.is.emotionull.com
  2. 2. What is Clojure?
  3. 3. Ok, what is Lisp?
  4. 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. 5. “LISP stands for: Lots of Insane Stupid Parentheses” Anonymous
  6. 6. The Truth about Lisp
  7. 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. 8. Clojure● Lisp in JVM● Concurrent programming● Dynamic Development (REPL)● Lazy sequences● No side effects (almost)
  9. 9. Enter Clojure
  10. 10. Everything is code
  11. 11. (println "Hello World")function argument
  12. 12. Everything is data
  13. 13. (println "Hello World")list symbol string
  14. 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. 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. 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. 17. clojure might be a better java than java
  18. 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. 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. 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. 21. public isBlank(str) { if (str == null || (strLen = str.length()) == 0) { return true; } every (ch in str) { Character.isWhitespace(ch); } return true;}
  22. 22. public isBlank(str) { every (ch in str) { Character.isWhitespace(ch); }}
  23. 23. (defn blank? [s] (every? #(Character/isWhitespace %) s))
  24. 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. 25. Clojure is a functional language● Functions are first-class objects● Data is immutable● Functions are pure
  26. 26. So what?
  27. 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. 28. user=> (println "hello world")| hello world-> nil
  29. 29. user=> (defn hello [name] (str "Hello, " name))#user/hello
  30. 30. (hello "Clojure")
  31. 31. (.toUpperCase “hello”)
  32. 32. (str “hello” “ “ “world”)
  33. 33. (+ 1 3 4 (* 5 6))
  34. 34. (defn greeting"Returns a greeting of the form Hello, username."[username](str "Hello, " username))
  35. 35. (greeting "world")
  36. 36. user=> (doc greeting) ------------------------- exploring/greeting ([username])Returns a greeting of the form Hello, username.
  37. 37. (defn is-small? [number] (if (< number 100) "yes" ))
  38. 38. (is-small? 50) "yes"
  39. 39. (is-small? 50000) nil
  40. 40. (defn is-small? [number] (if (< number 100) "yes" "no" ))
  41. 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. 42. Java in Clojure● Yeap
  43. 43. Clojure in Java● Yeap
  44. 44. Clojure in Clojure● Yeap, yeap
  45. 45. Java in Clojurejava new Widget(“foo”)clojure (Widget. “foo”)java dialog.show()clojure (.show dialog)
  46. 46. Tools● Emacs + SLIME● ViMClojure● Enclojure + Intellij IDEA● Counterclockwise + Eclipse● Leiningen
  47. 47. Clojure is not a just a new language
  48. 48. Is another, simplier way to solve problems
  49. 49. Thanks! Questions? @jonromero
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×