Clojure made-simple - John Stevenson

  • 1,820 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,820
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
0
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. AssumptionsSome experience with Java development Little or no experience with ClojureYou want to try something different!Haskell, Erlang andScala seem hairy!
  • 2. Why Clojure ?
  • 3. Why get functional ?4 cores in a typical developers Mac book ProHow long until 128 coresin a laptop is common?
  • 4. Parallelism made simplerProvides tools to create massively parallelapplications Not a panacea
  • 5. Think differentlyFunctional concepts Changing state
  • 6. What is Clojure
  • 7. One of many JVM languages Java Groovy Jruby Jython Scala Clojure
  • 8. A little more ubiquitous.Net framework – DLR/CLR In browsers > ClojureScriptNative compilers
  • 9. Very small syntax ( ) [ ]def, defn, defprojectlist, map, vector, set let # _ atom
  • 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. A better Lisp ! Sensible () usage Sensible macro names JVM InteroperabilityLisp is the 2nd oldest programming language still in use
  • 12. Which LISP is your wingman ?Common Lisp Clojure
  • 13. Clojure is modular
  • 14. The dark side of Clojure (x)
  • 15. The dark side of Clojure ((x))
  • 16. The dark side of Clojure (((x)))
  • 17. The dark side of Clojure((((x))))
  • 18. The dark side of Clojure(((((x)))))
  • 19. ...verses non-lisp languages () verses { () };
  • 20. Well nearly....([] ((()))) verses{ ({([])}) };
  • 21. Comparing Clojure with Java
  • 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. Its all byte code in the end..Any object in Clojure is just a regular javaobjectTypes inherit from: java.lang.object
  • 24. Prefix notation3 + 4 + 5 – 6 / 3(+ 3 4 5 (- (/ 6 3)))
  • 25. Prefix notation everywhere(defn square-the-number[x] (* x x))
  • 26. Defining a data structure( def my-data-structure [ data ] )( def days-of-the-week [“Monday” “Tuesday” “Wednesday”])
  • 27. Really simple data structure(def jr0cket {:first-name "John", :last-name "Stevenson"})
  • 28. Defining simple data(def name data)
  • 29. Calling Behaviour(function-name data data)
  • 30. ImmutableData structures
  • 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. Vectors – hashed ordered list[:matrix-characters [:neo :morpheus :trinity :smith]](first [:neo :morpheus :trinity :smith])(nth [:matrix :babylon5 :firefly] 2)(concat [:neo] [:trinity])
  • 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. 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. A few interestingClojure examples
  • 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. Java made simpler// Javaimport java.util.Date;{Date currentDate = new Date();}; Clojure(import java.util.Date)(new Date)
  • 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. Calling Java GUI(javax.swing.JOptionPane/showMessageDialog nil "Hello JAX London" )
  • 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. What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hellocruel world")))clojure.lang.Var
  • 42. Get coding !
  • 43. clojure.orgdocs.clojure.org
  • 44. All hail the REPLAn interactive shell forclojureFast feedback loopfor clojure
  • 45. Managing a Clojure project
  • 46. MavenJust like any other Java projectStep 1)Add Clojure library dependency to yourPOMStep 2)Download the Internet !!!
  • 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. Emacs
  • 49. Light tableKickstarter project to build a development environment, focused on the developer experience
  • 50. Functional Web
  • 51. webnoir.org
  • 52. Clojure calling Java web stuff(let [conn] (doto (HttpUrlConnection. Url) (.setRequestMethod “POST”) (.setDoOutput true) (.setInstaneFollowRedirects true))])
  • 53. Getting a little more functional
  • 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. Tail recursion(defn recursive-counter (print answer) (if (< answer 1000) (recur (+ answer 4))))
  • 56. Where to find out more... clojure.org/ cheatsheet
  • 57. Hacking Clojure challenges
  • 58. Mutable State
  • 59. Software Transactional MemoryProvides safe,concurrent access tomemoryAgents allowencapsulated accessto mutable resources http://www.studiotonne.com/illustration/software-transactional-memory/
  • 60. Coding state changes● Atoms● Try / catch ; Clojure atom code (def mouseposition (atom [0 0])) [mx my] @mouseposition (reset! mouseposition [x y])
  • 61. Database accessClojure-contrib has sqllibraryKorma - "Tasty SQL forClojure"CongoMongo forMongoDBhttps://devcenter.heroku.com/articles/clojure-web-application
  • 62. Getting Creative with Clojure
  • 63. Overtone: Music to your ears
  • 64. Visuals to your ears● Quil – clojure library for processing
  • 65. Thank you London Clojurian Google Group@jr0cket