AssumptionsSome experience with Java development             Little or no experience with ClojureYou want to try something...
Why Clojure ?
Why get functional ?4 cores in a typical developers Mac book ProHow long until 128 coresin a laptop is common?
Parallelism made simplerProvides tools to create massively parallelapplications                                  Not a pan...
Think differentlyFunctional concepts                           Changing state
What is Clojure
One of many JVM languages          Java         Groovy          Jruby         Jython         Scala         Clojure
A little more ubiquitous.Net framework – DLR/CLR                   In browsers > ClojureScriptNative compilers
Very small syntax         ( )         [ ]def, defn, defprojectlist, map, vector, set         let          #          _    ...
It uses mathematical concepts    Functions have a value For a given set of arguments,you should always get the same       ...
A better Lisp !   Sensible () usage   Sensible macro names   JVM InteroperabilityLisp is the 2nd oldest programming langua...
Which LISP is your wingman ?Common Lisp          Clojure
Clojure is modular
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            ()           verses      { () };
Well nearly....([] ((())))       verses{ ({([])}) };
Comparing Clojure   with Java
Clojure conceptsEncourages Pure Functional approach- use STM to change stateFunctions as first class citizens  - functions...
Its all byte code in the end..Any object in Clojure is just a regular javaobjectTypes inherit from:        java.lang.object
Prefix notation3 + 4 + 5 – 6 / 3(+ 3 4 5 (- (/ 6 3)))
Prefix notation everywhere(defn square-the-number[x]  (* x x))
Defining a data structure( def my-data-structure [ data ] )( def days-of-the-week  [“Monday” “Tuesday” “Wednesday”])
Really simple data structure(def jr0cket  {:first-name "John",   :last-name "Stevenson"})
Defining simple data(def name data)
Calling Behaviour(function-name data data)
ImmutableData structures
List – Ordered collection(list 1 3 5 7)(1 3 5 7)(quote (1 3 5 7)) (1 2 3) ;   1 is not a function
Vectors – hashed ordered list[:matrix-characters [:neo :morpheus :trinity :smith]](first [:neo :morpheus :trinity :smith])...
Maps – unordered key/values{:a 1 :b 2}                  {:a {:a 1}}  {:a 1, :b 2}                 {:a {:a 1}}{ :a 1 :b }  ...
Lists are for code                        and dataVectors are for data               and arguments Its not that black and ...
A few interestingClojure examples
RatioUnique data type           (/ 2 4)                           (/ 2.0 4)Allow lazy evaluation                          ...
Java made simpler// Javaimport java.util.Date;{Date currentDate = new Date();}; Clojure(import java.util.Date)(new Date)
Joda Time(use clj-time.core)(date-time 1986 10 14)(hour (date-time 1986 10 14 22))(from-time-zone (date-time 2012 10 17)  ...
Calling Java GUI(javax.swing.JOptionPane/showMessageDialog nil    "Hello JAX London" )
Working with JavaJava Classes●   fullstop after class name    (JFrame. )    (Math/cos 3) ; static method callJava methods●...
What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hellocruel world")))cloj...
Get coding !
clojure.orgdocs.clojure.org
All hail the REPLAn interactive shell forclojureFast feedback loopfor clojure
Managing a Clojure     project
MavenJust like any other Java projectStep 1)Add Clojure library dependency to yourPOMStep 2)Download the Internet !!!
Leiningen                     leiningen.orglein new       ●   Create a new Clojure projectlein deps      ●   Download all ...
Emacs
Light tableKickstarter project to build a development environment,          focused on the developer experience
Functional Web
webnoir.org
Clojure calling Java web stuff(let [conn]  (doto (HttpUrlConnection. Url)   (.setRequestMethod “POST”)   (.setDoOutput tru...
Getting a little more functional
Recursive functions●   Functions that call   ●   Tail recursion    themselves            ●   Avoids blowing the           ...
Tail recursion(defn recursive-counter  (print answer)  (if (< answer 1000)   (recur (+ answer 4))))
Where to find out more...  clojure.org/   cheatsheet
Hacking Clojure challenges
Mutable State
Software Transactional MemoryProvides safe,concurrent access tomemoryAgents allowencapsulated accessto mutable resources  ...
Coding state changes●   Atoms●   Try / catch    ; Clojure atom code    (def mouseposition (atom [0 0]))    [mx my] @mousep...
Database accessClojure-contrib has sqllibraryKorma - "Tasty SQL forClojure"CongoMongo forMongoDBhttps://devcenter.heroku.c...
Getting Creative  with Clojure
Overtone: Music to your ears
Visuals to your ears●   Quil – clojure library for processing
Thank you             London Clojurian                Google Group@jr0cket
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Clojure made-simple - John Stevenson
Upcoming SlideShare
Loading in...5
×

Clojure made-simple - John Stevenson

2,042

Published on

There is an increasing interest in functional programming from Java developers and the organisations in which they work. For many companies the challenge now is how to make use of the competitive advantage of functional programming. For developers, how do you adapt your mindset to this newly reimagined paradigm? Through the use of examples and a modular approach to design, Clojure made simple will show how developers can be productive quickly without a major change to their current development life-cycle. We will also cover the Clojure build process, tools and exciting projects out there.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,042
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Clojure made-simple - John Stevenson"

  1. 1. AssumptionsSome experience with Java development Little or no experience with ClojureYou want to try something different!Haskell, Erlang andScala seem hairy!
  2. 2. Why Clojure ?
  3. 3. Why get functional ?4 cores in a typical developers Mac book ProHow long until 128 coresin a laptop is common?
  4. 4. Parallelism made simplerProvides tools to create massively parallelapplications Not a panacea
  5. 5. Think differentlyFunctional concepts Changing state
  6. 6. What is Clojure
  7. 7. One of many JVM languages Java Groovy Jruby Jython Scala Clojure
  8. 8. A little more ubiquitous.Net framework – DLR/CLR In browsers > ClojureScriptNative compilers
  9. 9. Very small syntax ( ) [ ]def, defn, defprojectlist, map, vector, set let # _ atom
  10. 10. It uses mathematical concepts Functions have a value For a given set of arguments,you should always get the same result (Referential transparency)
  11. 11. A better Lisp ! Sensible () usage Sensible macro names JVM InteroperabilityLisp is the 2nd oldest programming language still in use
  12. 12. Which LISP is your wingman ?Common Lisp Clojure
  13. 13. Clojure is modular
  14. 14. The dark side of Clojure (x)
  15. 15. The dark side of Clojure ((x))
  16. 16. The dark side of Clojure (((x)))
  17. 17. The dark side of Clojure((((x))))
  18. 18. The dark side of Clojure(((((x)))))
  19. 19. ...verses non-lisp languages () verses { () };
  20. 20. Well nearly....([] ((()))) verses{ ({([])}) };
  21. 21. Comparing Clojure with Java
  22. 22. 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
  23. 23. Its all byte code in the end..Any object in Clojure is just a regular javaobjectTypes inherit from: java.lang.object
  24. 24. Prefix notation3 + 4 + 5 – 6 / 3(+ 3 4 5 (- (/ 6 3)))
  25. 25. Prefix notation everywhere(defn square-the-number[x] (* x x))
  26. 26. Defining a data structure( def my-data-structure [ data ] )( def days-of-the-week [“Monday” “Tuesday” “Wednesday”])
  27. 27. Really simple data structure(def jr0cket {:first-name "John", :last-name "Stevenson"})
  28. 28. Defining simple data(def name data)
  29. 29. Calling Behaviour(function-name data data)
  30. 30. ImmutableData structures
  31. 31. List – Ordered collection(list 1 3 5 7)(1 3 5 7)(quote (1 3 5 7)) (1 2 3) ; 1 is not a function
  32. 32. Vectors – hashed ordered list[:matrix-characters [:neo :morpheus :trinity :smith]](first [:neo :morpheus :trinity :smith])(nth [:matrix :babylon5 :firefly] 2)(concat [:neo] [:trinity])
  33. 33. Maps – unordered key/values{:a 1 :b 2} {:a {:a 1}} {:a 1, :b 2} {:a {:a 1}}{ :a 1 :b } {{:a 1} :a}java.lang.ArrayIndexOutOfB {{:a 1} :a}oundsException: 3 ; idiom - put :a on the left{ :a 1 :b 2} {:a 1, :b 2}
  34. 34. Lists are for code and dataVectors are for data and arguments Its not that black and white, but its a useful starting point when learning
  35. 35. A few interestingClojure examples
  36. 36. RatioUnique data type (/ 2 4) (/ 2.0 4)Allow lazy evaluation (/ 1 3)Avoid loss of (/ 1.0 3)precision (class (/ 1 3)
  37. 37. Java made simpler// Javaimport java.util.Date;{Date currentDate = new Date();}; Clojure(import java.util.Date)(new Date)
  38. 38. Joda Time(use clj-time.core)(date-time 1986 10 14)(hour (date-time 1986 10 14 22))(from-time-zone (date-time 2012 10 17) (time-zone-for-offset -8))(after? (date-time 1986 10) (date-time 1986 9))(show-formatters)
  39. 39. Calling Java GUI(javax.swing.JOptionPane/showMessageDialog nil "Hello JAX London" )
  40. 40. Working with JavaJava Classes● fullstop after class name (JFrame. ) (Math/cos 3) ; static method callJava methods● fullstop before method name (.getContentPane frame) ;;method name first (. frame getContentPane) ;;object first
  41. 41. What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hellocruel world")))clojure.lang.Var
  42. 42. Get coding !
  43. 43. clojure.orgdocs.clojure.org
  44. 44. All hail the REPLAn interactive shell forclojureFast feedback loopfor clojure
  45. 45. Managing a Clojure project
  46. 46. MavenJust like any other Java projectStep 1)Add Clojure library dependency to yourPOMStep 2)Download the Internet !!!
  47. 47. Leiningen leiningen.orglein new ● Create a new Clojure projectlein deps ● Download all dependencieslein repl ● Start the interactive shell (repl)lein jack-in ● Start a REPL server
  48. 48. Emacs
  49. 49. Light tableKickstarter project to build a development environment, focused on the developer experience
  50. 50. Functional Web
  51. 51. webnoir.org
  52. 52. Clojure calling Java web stuff(let [conn] (doto (HttpUrlConnection. Url) (.setRequestMethod “POST”) (.setDoOutput true) (.setInstaneFollowRedirects true))])
  53. 53. Getting a little more functional
  54. 54. Recursive functions● Functions that call ● Tail recursion themselves ● Avoids blowing the stack● Fractal coding ● A trick as the JVM does not support tail recursion directly :-(
  55. 55. Tail recursion(defn recursive-counter (print answer) (if (< answer 1000) (recur (+ answer 4))))
  56. 56. Where to find out more... clojure.org/ cheatsheet
  57. 57. Hacking Clojure challenges
  58. 58. Mutable State
  59. 59. Software Transactional MemoryProvides safe,concurrent access tomemoryAgents allowencapsulated accessto mutable resources http://www.studiotonne.com/illustration/software-transactional-memory/
  60. 60. Coding state changes● Atoms● Try / catch ; Clojure atom code (def mouseposition (atom [0 0])) [mx my] @mouseposition (reset! mouseposition [x y])
  61. 61. Database accessClojure-contrib has sqllibraryKorma - "Tasty SQL forClojure"CongoMongo forMongoDBhttps://devcenter.heroku.com/articles/clojure-web-application
  62. 62. Getting Creative with Clojure
  63. 63. Overtone: Music to your ears
  64. 64. Visuals to your ears● Quil – clojure library for processing
  65. 65. Thank you London Clojurian Google Group@jr0cket

×