• Save
Clojure made-simple - John Stevenson
Upcoming SlideShare
Loading in...5

Clojure made-simple - John Stevenson



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.



Total Views
Views on SlideShare
Embed Views



3 Embeds 35

http://www.slashdocs.com 16
http://lanyrd.com 13
http://www.pearltrees.com 6



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Clojure made-simple - John Stevenson Clojure made-simple - John Stevenson Presentation Transcript

  • AssumptionsSome experience with Java development Little or no experience with ClojureYou want to try something different!Haskell, Erlang andScala seem hairy!
  • 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 panacea
  • 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 # _ atom
  • It uses mathematical concepts Functions have a value For a given set of arguments,you should always get the same result (Referential transparency)
  • A better Lisp ! Sensible () usage Sensible macro names JVM InteroperabilityLisp is the 2nd oldest programming language still in use
  • 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 as arguments as they return a valueMake JVM interoperation simple - easy to use your existing Java applications
  • 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])(nth [:matrix :babylon5 :firefly] 2)(concat [:neo] [:trinity])
  • 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}
  • 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
  • A few interestingClojure examples
  • RatioUnique data type (/ 2 4) (/ 2.0 4)Allow lazy evaluation (/ 1 3)Avoid loss of (/ 1.0 3)precision (class (/ 1 3)
  • 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) (time-zone-for-offset -8))(after? (date-time 1986 10) (date-time 1986 9))(show-formatters)
  • 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● fullstop before method name (.getContentPane frame) ;;method name first (. frame getContentPane) ;;object first
  • What class is that...(class (str "Jr0cket"))java.lang.String(class (defn hello-world [name] (str "Hellocruel world")))clojure.lang.Var
  • 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 dependencieslein repl ● Start the interactive shell (repl)lein jack-in ● Start a REPL server
  • 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 true) (.setInstaneFollowRedirects true))])
  • Getting a little more functional
  • 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 :-(
  • 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 http://www.studiotonne.com/illustration/software-transactional-memory/
  • Coding state changes● Atoms● Try / catch ; Clojure atom code (def mouseposition (atom [0 0])) [mx my] @mouseposition (reset! mouseposition [x y])
  • Database accessClojure-contrib has sqllibraryKorma - "Tasty SQL forClojure"CongoMongo forMongoDBhttps://devcenter.heroku.com/articles/clojure-web-application
  • 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