( s im p le   ( c lo ju r e ) )C lo ju r e m a d e     s im p le                       Ta l k
W h a t isC lo ju r e
Clojure is small and flexible
Clojure conceptsEncourages Pure Functional approach- use STM to change stateFunctions as first class citizens  - functions...
A better Lisp !Sensible () usageSensible macro namesJVM Interoperability
Which LISP is your wingman ?Common Lisp        Clojure
The dark side of Clojure         ( x )
The dark side of Clojure      ( ( x ) )
The dark side of Clojure    ( ( ( x ) ) )
The dark side of Clojure ( ( ( ( x ) ) ) )
The dark side of Clojure( ( ( ( ( x ) ) ) ) )
...verses non-lisp languages      ( ) ==     { ( ) };
Well nearly.... ([] ((())))        =={ ( {( []) }) };
C o m p a r in g    J a va    w it h  C lo ju r e
It s a ll b y t e c o d e in t h e               e nd ..Any object in clojure is just a regular java objectA reference typ...
B a s icC lo ju r eS yn ta x
Name-spaces; Define a namespace  ( ns nam s pac e- nam          e-           e); Include namespace code  ( us e  nam - s p...
Defining a function First principles            Syntactic Sugar ( def nam   e               ( def n nam e    ( f n [ ar gs...
Special formsSpecial evaluation rules for the Clojure compiler  - part of the language definitioncatch, def, do, dot (.), ...
Simple function exampleDefine a simple function( def n hel l o- w l d [ nam                      or          e]   ( pr i n...
Prefix notation( def n s quar e- t he- number  [ x]    ( * x x) )
“Overloading” functions( def n make   ([ ]    ; do       t h e f o l l o wi n g f u n c t i o n i f n o ar g s        ( s ...
Im m u t a b le    D a tas truc ture s
List – Ordered collection( l i s t 1 3 5 7) ( 1 3 5 7) ( 1 2 3) ; 1 i s n o t a  f unct i on
Vectors – hashed ordered list[ : m r i x- c har ac t er s [ : neo     at   : m pheus : t r i ni t y : s m t h] ]      or  ...
Maps – unordered key/values{ : a 1 : b 2}                                { : a { : a 1} }    { : a 1 , : b 2}             ...
L is t s a r e f o r       c odeVe c t o r s a r e  fo r d a ta
Working with data(last [1 1 2 3 5 8])        (doc last)(defn penultimate [x] (last (butlast x)) )       (doc butlast)(penu...
And more...(nth [1 1 2 3 5 8] 2)(count [1 1 2 3 5 8])(reverse [1 1 2 3 5 8])(defn palindrome? [x]     Proposition – naming...
Even more(flatten [[1 1] 2 [3 [5 8]]])(compress "aaaabccaadeeee")(encode "aaaabccaadeeee")(replicate 10 "a")
Defining a data structure( def m dat a- s t r uc t ur e        y- [ dat a ] )( def days - of - t he- week  [ “Monday” “ Tu...
Example data structure( def j r 0c ket   { : f i r s t - nam " J ohn" ,                      e    : l as t - name " St eve...
G e t c o d in g !
c lo ju r e .           orgd o c s . c lo ju r     e .o rg
All hail the REPLAn interactive shell for clojureFast feedback loop for clojure
M a n a g in g a   c lo ju r e   p r o je c t
MavenJust like any other Java projectStep 1)Add Clojure library jar to your POMStep 2)Download the Internet !!!
le in in g e n   Leiningen                               .o rglein new     Create a new clojure projectlein deps    Downlo...
Ema c s
A fe win t e r e s t in g   C lo ju r e e x a m p le s
str(str h e l l o)         Concatenate strings                              together                               represe...
RatioUnique data type            ( / 3 5)                            ( / 3. 0Allow lazy evaluation                        ...
Calling Java... ooooo!!( j avax . s wi n g . JOp t i o n Pan e /    s h o wMe s s ag e D i al o g n i l        " He l l o ...
Importing Java into Clojure( ns dr aw ng- dem          i       o   ( : i m t [ j avax. s w ng J panel          por        ...
Working with JavaJava Classes  fullstop after class name  ( J Fr am )           e.  (Math/cos 3) ; static method callJava ...
What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hello  cruel world")))cl...
Clojure calling Java web stuff( l et [ c onn]    ( dot o ( Ht t pUr l Connec t i on.  Ur l )       ( . s et Reques t M hod...
Recursive functionsFunctions that call   Tail recursion themselves           Avoids blowing the                       stac...
Tail recursion( def n r ec ur s i ve- c ount er   ( pr i nt ans w )er   ( i f ( < ans w  er 1000)     (   r ec ur   ( + an...
Where to find out more...c l oj ur e. or g/ c heat s h             eet
M u t a b le S t a t e
Software Transactional MemoryProvides safe, concurrent access to memoryAgents allow encapsulated access to mutable resources
Functional for the webBig blog of stuff in > Big blob of stuff outEncourages modularising responsibilitiesGood at processi...
Noir   w e b n o ir . o r g
M o re to             c o m e ...          c l oj ur e. or g       dev. c l oj ur e. or g              @ r 0c ket         ...
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Clojure made simple - Lightning talk
Upcoming SlideShare
Loading in …5
×

Clojure made simple - Lightning talk

1,399 views

Published on

A very simple overview of some Clojure characteristics and syntax to help you get going with the language

Published in: Technology, Education
  • Be the first to comment

Clojure made simple - Lightning talk

  1. 1. ( s im p le ( c lo ju r e ) )C lo ju r e m a d e s im p le Ta l k
  2. 2. W h a t isC lo ju r e
  3. 3. Clojure is small and flexible
  4. 4. Clojure conceptsEncourages Pure Functional approach- use STM to change stateFunctions as first class citizens - functions as arguments as they return a valueMake JVM interoperation simple - easy to use your existing Java applications
  5. 5. A better Lisp !Sensible () usageSensible macro namesJVM Interoperability
  6. 6. Which LISP is your wingman ?Common Lisp Clojure
  7. 7. The dark side of Clojure ( x )
  8. 8. The dark side of Clojure ( ( x ) )
  9. 9. The dark side of Clojure ( ( ( x ) ) )
  10. 10. The dark side of Clojure ( ( ( ( x ) ) ) )
  11. 11. The dark side of Clojure( ( ( ( ( x ) ) ) ) )
  12. 12. ...verses non-lisp languages ( ) == { ( ) };
  13. 13. Well nearly.... ([] ((()))) =={ ( {( []) }) };
  14. 14. C o m p a r in g J a va w it h C lo ju r e
  15. 15. It s a ll b y t e c o d e in t h e e nd ..Any object in clojure is just a regular java objectA reference type inheriting from: j ava. l ang. obj ec t
  16. 16. B a s icC lo ju r eS yn ta x
  17. 17. Name-spaces; Define a namespace ( ns nam s pac e- nam e- e); Include namespace code ( us e nam - s pac e- nam es e)Groups code together like a package statement in Java
  18. 18. Defining a function First principles Syntactic Sugar ( def nam e ( def n nam e ( f n [ ar gs ] [ ar gs ] ( f unc t i on- body ( f unc t i on- …) ) ) body . . . ) )( def n w c om m s age [ nam ] el e- es e ( pr i nt l n ( s t r “ W c om t o Cl oj ur e ” el enam )e)
  19. 19. Special formsSpecial evaluation rules for the Clojure compiler - part of the language definitioncatch, def, do, dot (.), finally, fn, if, let, loop, monitor-enter, monitor-exit, new, quote, recur, set!, throw, try and var
  20. 20. Simple function exampleDefine a simple function( def n hel l o- w l d [ nam or e] ( pr i nt l n( s t r " Hel l o " nam ) ) e)Call the function in the REPL or in your code( hel l o- w l d " j r 0c ket " ) or
  21. 21. Prefix notation( def n s quar e- t he- number [ x] ( * x x) )
  22. 22. “Overloading” functions( def n make ([ ] ; do t h e f o l l o wi n g f u n c t i o n i f n o ar g s ( s t r uc t vec t or 0 0) ) ( [ x y] ; d o i f x an d y ar e ar g u me n t s ( s t r uc t vec t or x y) ) )
  23. 23. Im m u t a b le D a tas truc ture s
  24. 24. List – Ordered collection( l i s t 1 3 5 7) ( 1 3 5 7) ( 1 2 3) ; 1 i s n o t a f unct i on
  25. 25. Vectors – hashed ordered list[ : m r i x- c har ac t er s [ : neo at : m pheus : t r i ni t y : s m t h] ] or i( f i r s t [ : n e o : mo r p h e u s : t r i n i t y : s mi t h ] )( nt h [ : mat r i x : b ab yl o n 5 : f i r e f l y : s t ar g at e ] 2 )( c onc at [ : n e o ] [ : t r i n i t y] )
  26. 26. Maps – unordered key/values{ : a 1 : b 2} { : a { : a 1} } { : a 1 , : b 2} {: a {: a 1}}{ :a 1 :b } { { : a 1} : a}j ava. l an g . Ar r ayI n d e x Ou t Of Bo { { : a 1 } : a} u n d s Ex c e p t i o n : 3 ; i d i om - put : a on t he left{ : a 1 : b 2} { : a 1 , : b 2}
  27. 27. L is t s a r e f o r c odeVe c t o r s a r e fo r d a ta
  28. 28. Working with data(last [1 1 2 3 5 8]) (doc last)(defn penultimate [x] (last (butlast x)) ) (doc butlast)(penultimate [1 2 3 4 5])
  29. 29. And more...(nth [1 1 2 3 5 8] 2)(count [1 1 2 3 5 8])(reverse [1 1 2 3 5 8])(defn palindrome? [x] Proposition – naming (= x (reverse x)) ) convention
  30. 30. Even more(flatten [[1 1] 2 [3 [5 8]]])(compress "aaaabccaadeeee")(encode "aaaabccaadeeee")(replicate 10 "a")
  31. 31. Defining a data structure( def m dat a- s t r uc t ur e y- [ dat a ] )( def days - of - t he- week [ “Monday” “ Tues day” “W ednes day” ] )
  32. 32. Example data structure( def j r 0c ket { : f i r s t - nam " J ohn" , e : l as t - name " St evens on" } )
  33. 33. G e t c o d in g !
  34. 34. c lo ju r e . orgd o c s . c lo ju r e .o rg
  35. 35. All hail the REPLAn interactive shell for clojureFast feedback loop for clojure
  36. 36. M a n a g in g a c lo ju r e p r o je c t
  37. 37. MavenJust like any other Java projectStep 1)Add Clojure library jar to your POMStep 2)Download the Internet !!!
  38. 38. le in in g e n Leiningen .o rglein new Create a new clojure projectlein deps Download all dependencieslein repl Start the interactive shell (repl)lein swank Start repl server
  39. 39. Ema c s
  40. 40. A fe win t e r e s t in g C lo ju r e e x a m p le s
  41. 41. str(str h e l l o) Concatenate strings together represent a character using (def data “boo”) Join arguments with(str “message” data ) strings
  42. 42. RatioUnique data type ( / 3 5) ( / 3. 0Allow lazy evaluation 5)Avoid loss of precision (/ 1 3) (/ 1.0 3) (class (/ 1
  43. 43. Calling Java... ooooo!!( j avax . s wi n g . JOp t i o n Pan e / s h o wMe s s ag e D i al o g n i l " He l l o W r l d " ) o
  44. 44. Importing Java into Clojure( ns dr aw ng- dem i o ( : i m t [ j avax. s w ng J panel por i J Fr am e] [ j ava. awt Di m i on] ) ) ens
  45. 45. Working with JavaJava Classes fullstop after class name ( J Fr am ) e. (Math/cos 3) ; static method callJava methods fullstop before method name ( . get Cont ent Pane f r am ;;method name first e) ( . f r am get Cont ent Pane) ;;object first e
  46. 46. What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hello cruel world")))clojure.lang.Var
  47. 47. Clojure calling Java web stuff( l et [ c onn] ( dot o ( Ht t pUr l Connec t i on. Ur l ) ( . s et Reques t M hod et “ POST” ) ( . s et DoOut put t r ue) ( . s et I ns t aneFol l ow Redi r ec t s t r ue) ) ] )
  48. 48. Recursive functionsFunctions that call Tail recursion themselves Avoids blowing the stackFractal coding A trick as the JVM does not support tail recursion directly :-(
  49. 49. Tail recursion( def n r ec ur s i ve- c ount er ( pr i nt ans w )er ( i f ( < ans w er 1000) ( r ec ur ( + ans wer 4) ) ) )
  50. 50. Where to find out more...c l oj ur e. or g/ c heat s h eet
  51. 51. M u t a b le S t a t e
  52. 52. Software Transactional MemoryProvides safe, concurrent access to memoryAgents allow encapsulated access to mutable resources
  53. 53. Functional for the webBig blog of stuff in > Big blob of stuff outEncourages modularising responsibilitiesGood at processing data in parallel
  54. 54. Noir w e b n o ir . o r g
  55. 55. M o re to c o m e ... c l oj ur e. or g dev. c l oj ur e. or g @ r 0c ket jc l oj ur e. j r 0c ket . c o

×