Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

I know Java, why should I consider Clojure?


Published on

Avram Aelony presented this talk in SBJUG on September 27 2012.

To introduce Clojure as a powerful JVM language and look at Clojure from a value-added perspective for those that already know Java.

The recorded talk can be found at -

Published in: Technology
  • Be the first to comment

  • Be the first to like this

I know Java, why should I consider Clojure?

  1. 1. Introducing Clojure as a powerful JVM language from a value-added perspective for those that already know Java. a.k.a. “Okay, I know Java, why should I consider Clojure?”
  2. 2. A little about me● Avram Aelony● My programming language evolution● Notes about evangelism ● use the language you prefer “whenever, wherever...” like Shakira says● How I came to Clojure
  3. 3. Disclaimers● Standing on the shoulders of giants, luminaries● This is a HUGE topic● The Good news: ● There are MUCH better (explained, comprehensive, deeper) talks on this topic than mine ● I will reference them ● Should you remain unconvinced by this talk, there are inumerable resources online that may be somewhat more convincing
  4. 4. Audience survey● Java ?● Clojure ?● Other functional language?● Other JVM language?
  5. 5. Utility● Why is Clojure useful if you know Java? ● It adds to what you know ● Provides simplicity, concision, more l
  6. 6. Grand tour● Idea is to go into the Rationale and introduce Clojure along the way● Mostly about the Why rather than the How ● ... some How as well...
  7. 7. Preliminaries and SimilaritiesSource: "Clojure-Java Interop: A Better Java than Java"
  8. 8. Rationale "I wanted: A Lisp for Functional Programming symbiotic with an established Platform designed for Concurrency."● - Rich Hickey, creator of Clojure
  9. 9. Break down1. A Lisp2. for Functional Programming3. symbiotic with an established platform4. designed for Concurrency
  10. 10. Break down1. A Lisp2. for Functional Programming3. symbiotic with an established platform4. designed for Concurrency
  11. 11. A Lisp● Dynamic typing● Homoiconic: Uniform, elegant syntax● Expression oriented: Symbolic expressions● Lambda calculus: variable binding● Macros● Code as data● Data as code
  12. 12. Clojure Data Structures● Lists ( 1 2 3 4 ) ● new insertions go to the front● Vectors [ 1 2 3 4 ] ● new insertions go to the back● Maps { :a 1 :b 2 :c 3 :d 4}● Sets #{ :a :b :c :d } ● implemented as k/v where k=v.
  13. 13. Data Structures
  14. 14. Persistent Data Structures
  15. 15. Immutable Data● So how can things change? ● New things can be created ● Underlying structure is shared wherever possible ● only connections change
  16. 16. Immutable Data + Structural Sharing identical? function that returns true only when symbols are in fact the same object. Phillip Potter
  17. 17. Collections Abstraction● Vectors, Maps, Sets can be thought of as Collections.● Most functions that work on one data structure will work on any other.● Easy to change from Vector to Map to Set with minimal refactoring of functions.
  18. 18. Sequences and Collections● seqs are persistent and immutable● seq function● lazyiness, lazy application● seq interface
  19. 19. Seqs
  20. 20. Homo-iconicHomo = Same, Iconic = representation
  21. 21. Anonymous Function syntax
  22. 22. Fizz BuzzPrint the numbers from 1 to NIf a number is divisible by 3, print "Fizz" insteadIf a number is divisible by 5, print "Buzz" insteadIf a number is divisible by 3 and 5, print "FizzBuzz" instead
  23. 23. Fizz Buzz
  24. 24. S-Expressions, data as code as seen in The Joy of Clojure
  25. 25. S-Expressions, data as code● John McCarthy● assign symbolic names to clojure data● trees of expressions, each of which returns a value● functions can be assigned to vars● def, fn, defn
  26. 26. Macros● Why Macros? ● to arrange code differently – Threading macros -> and ->> – infix versus postfix – dot and dot dot macros for Java interop ● to remove or reduce boilerplate code
  27. 27. “The whole language is always available. There is no real distinctionbetween read-time, compile-time, and runtime. You can compile or run codewhile reading, read or run code while compiling, and read or compile code atruntime.”“Running code at read-time lets users reprogram Lisps syntax; running codeat compile-time is the basis of macros; compiling at runtime is the basis ofLisps use as an extension language in programs like Emacs; and reading atruntime enables programs to communicate using s-expressions, an idearecently reinvented as XML. “ What Made Lisp Different
  28. 28. Compilation● “Clojure compiles all code you load on-the-fly into JVM bytecode, but sometimes it is advantageous to compile ahead-of-time (AOT).”
  29. 29. Code as Data
  30. 30. Code as data, Data as Code
  31. 31. Break down1. A Lisp2. for Functional Programming3. symbiotic with an established platform4. designed for Concurrency
  32. 32. Functional Programming● tools to avoid mutable state, data● referential transparency● functions are first class objects● emphasizes application of functions● emphasizes recursive iteration● encourages higher-order functions
  33. 33. Referential Transparency● expressions can be replaced with their value without changing the behavior of the program● easier to reason about programs “... can help in proving correctness, simplifying an algorithm, assisting in modifying code without breaking it, or optimizing code by means of memoization, common subexpression elimination or parallelization.” -wikipedia
  34. 34. Higher Order Functionsmap is an example of a higher order function, since itapplies another function to a collection.juxt is a higher order function that juxtaposes the valuesthat result from the application of one or more functions.
  35. 35. Not exactly what we want without map Higher order functions allow for great flexibility in re-shaping data.
  36. 36. Break down1. A Lisp2. for Functional Programming3. symbiotic with an established platform4. designed for Concurrency
  37. 37. JVM as host platform● Interop as built-in syntax● Java libraries easily used from Clojure● e.g. Hadoop, Apache libraries, anything in a Maven repo, etc..
  38. 38. Java Interop Clojure JavaConstructor (Widget. “foo”) new Widget(“foo”)Instance members (.nextInt rnd) rnd.nextInt()chaining access (.. person getAddress getZipCode) person.getAddress().getZipCode() (.getZipCode (.getAddress (person.)))static member access Math/PI Math.PI
  39. 39. Host Platforms● JVM● CLR / .NET● Javascript via Clojurescript
  40. 40. Break down1. A Lisp2. for Functional Programming3. symbiotic with an established platform4. designed for Concurrency
  41. 41. Designed for Concurrency● “I dont usually share State, but when I do...” ● Must explicitly use special symbols, functions to share mutable State.● Easier to use concurrency safely in Clojure
  42. 42. Designed for Concurrency● Asynchronous - the request to update is queued to happen in another thread sometime later. The thread that made the request can continue immediately.● Coordinated - reads and writes to multiple refs can be made in a way that guarantees no race conditions.● Retriable - work is speculative and may have to be repeated.
  43. 43. Concurrency vs Parallelism● Parallelism - partitioning of one task into multiple parts, each that run at the same time● Concurrency - execution of disparate tasks at roughly the same time, sharing a common resource
  44. 44. mutation a la carte● Available are Shared? Asynchronous? Coordinated? Retriable? Refs yes no yes yes Agents yes yes no no Atoms yes no no yes
  45. 45. Transactions● Software Transactional Memory (STM) ● (dosync ... ) ● STM uses Multiversion Concurrency Control – marks the old data as obsolete and adds the newer version –
  46. 46. Refs● mutable references to objects● can only be changed within a transaction (TX) ● (dosync ...)● no locks. no chance of a deadlock.● MVCC ensures snapshot isolation, each TX gets its own view of the data it is interested in.● each TX is oblivious to other TXs.● all ref modifications succeed or none do● If TX2 commits a change while TX1 is working, it may cause TX1 to be retried.
  47. 47. Refscommute This fn should be commutative, or, failing that, you must accept last-one-in-winsbehavior. commute allows for more concurrency than ref-set.
  48. 48. Refs
  49. 49. Atomsswap!reset!compare-and-set! - sets atom to new value if and only if current value ofthe atom is identical to the old value.
  50. 50. Agents
  51. 51. Tooling● Libraries● REPL programming● Leiningen ● project.clj● No IDE required, but many choices
  52. 52. Libraries Web: Ring, Noir, HTML: Hiccup, Enlive● Java libraries Hadoop: Cascalog, Statistics: Incanter SQL: Korma, CQL, Riak: Welle ● Maven etc... Office Documents: docjure & more...● Clojure libraries ●
  53. 53. REPL via Leiningen] lein new clj-excel && cd clj-excelnow edit file “project.clj”] lein deps && lein repl
  54. 54. REPL via Leiningen] lein deps] lein repl
  55. 55. Reading an Excel fileExcel file “sample.xlsx”
  56. 56. Reading an Excel file
  57. 57. IDEs and editors● Any editor with syntax highlighting will do● Your favorite Java IDE likely has a Clojure plugin● What do most folks use?
  58. 58. 1,372 responses were received over 7 days. Anounced via Twitter & Clojure mailing list (~6700 recipients)
  59. 59. That was a whirlwind grand tour... Thanks for listening! (def email {:name “aaelony” :domain “”})