i know java, why should i consider clojure?
DESCRIPTION
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 - http://www.youtube.com/watch?v=HhMCX8xwjo8TRANSCRIPT
![Page 1: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/1.jpg)
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?”
![Page 2: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/2.jpg)
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
![Page 3: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/3.jpg)
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
![Page 4: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/4.jpg)
Audience survey
● Java ?● Clojure ?● Other functional language?● Other JVM language?
![Page 5: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/5.jpg)
Utility● Why is Clojure useful if you know Java?
● It adds to what you know● Provides simplicity, concision, more
l
![Page 6: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/6.jpg)
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...
![Page 7: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/7.jpg)
Preliminaries and Similarities
Source: "Clojure-Java Interop: A Better Java than Java" http://www.infoq.com/presentations/Clojure-Java-Interop
![Page 8: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/8.jpg)
Rationale
"I wanted: A Lisp for Functional Programming symbiotic with an established Platform designed for Concurrency."
●
- Rich Hickey, creator of Clojure
http://clojure.org/rationale
![Page 9: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/9.jpg)
Break down
1. A Lisp
2. for Functional Programming
3. symbiotic with an established platform
4. designed for Concurrency
![Page 10: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/10.jpg)
Break down
1. A Lisp
2. for Functional Programming
3. symbiotic with an established platform
4. designed for Concurrency
![Page 11: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/11.jpg)
A Lisp
● Dynamic typing● Homoiconic: Uniform, elegant syntax● Expression oriented: Symbolic expressions● Lambda calculus: variable binding● Macros● Code as data● Data as code
![Page 12: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/12.jpg)
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.
![Page 13: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/13.jpg)
Data Structures
![Page 14: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/14.jpg)
Persistent Data Structures
![Page 15: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/15.jpg)
Immutable Data● So how can things change?
● New things can be created● Underlying structure is shared wherever possible● only connections change
![Page 16: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/16.jpg)
Immutable Data + Structural Sharing
Phillip Potter http://skillsmatter.com/podcast/scala/persistent-data-structures-in-clojure
identical?
function that returns true only when symbols are in fact the same object.
![Page 17: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/17.jpg)
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.
![Page 18: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/18.jpg)
Sequences and Collections
● seqs are persistent and immutable● seq function● lazyiness, lazy application● seq interface
![Page 19: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/19.jpg)
Seqs
![Page 20: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/20.jpg)
Homo-iconic
Homo = Same, Iconic = representation
![Page 21: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/21.jpg)
Anonymous Function syntax
![Page 22: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/22.jpg)
Fizz Buzz
Print the numbers from 1 to N
If a number is divisible by 3, print "Fizz" instead
If a number is divisible by 5, print "Buzz" instead
If a number is divisible by 3 and 5, print "FizzBuzz" instead
![Page 23: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/23.jpg)
Fizz Buzz
![Page 24: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/24.jpg)
S-Expressions, data as code
as seen in The Joy of Clojure
![Page 25: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/25.jpg)
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
http://en.wikipedia.org/wiki/Symbolic_expression
![Page 26: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/26.jpg)
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
![Page 27: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/27.jpg)
“The whole language is always available. There is no real distinction between read-time, compile-time, and runtime. You can compile or run code while reading, read or run code while compiling, and read or compile code at runtime.”
“Running code at read-time lets users reprogram Lisp's syntax; running code at compile-time is the basis of macros; compiling at runtime is the basis of Lisp's use as an extension language in programs like Emacs; and reading at runtime enables programs to communicate using s-expressions, an idea recently reinvented as XML. “
What Made Lisp Different http://www.paulgraham.com/diff.htm
![Page 28: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/28.jpg)
Compilation
● “Clojure compiles all code you load on-the-fly into JVM bytecode, but sometimes it is advantageous to compile ahead-of-time (AOT).”
http://clojure.org/compilation
![Page 29: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/29.jpg)
Code as Data
![Page 30: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/30.jpg)
Code as data, Data as Code
![Page 31: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/31.jpg)
Break down
1. A Lisp
2. for Functional Programming
3. symbiotic with an established platform
4. designed for Concurrency
![Page 32: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/32.jpg)
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
http://clojure.org/functional_programminghttp://en.wikipedia.org/wiki/Functional_programminghttp://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29
![Page 33: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/33.jpg)
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
http://en.wikipedia.org/wiki/Referential_transparency_%28computer_science%29
![Page 34: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/34.jpg)
Higher Order Functionsmap is an example of a higher order function, since it applies another function to a collection.
juxt is a higher order function that juxtaposes the values that result from the application of one or more functions.
![Page 35: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/35.jpg)
![Page 36: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/36.jpg)
Not exactly what we want without map
Higher order functions allow for great flexibility in re-shaping data.
![Page 37: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/37.jpg)
Break down
1. A Lisp
2. for Functional Programming
3. symbiotic with an established platform
4. designed for Concurrency
![Page 38: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/38.jpg)
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..
![Page 39: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/39.jpg)
Java InteropClojure Java
Constructor (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
![Page 40: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/40.jpg)
Host Platforms
● JVM● CLR / .NET● Javascript via Clojurescript
![Page 41: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/41.jpg)
Break down
1. A Lisp
2. for Functional Programming
3. symbiotic with an established platform
4. designed for Concurrency
![Page 42: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/42.jpg)
Designed for Concurrency
● “I don't usually share State, but when I do...”● Must explicitly use special symbols, functions to
share mutable State. ● Easier to use concurrency safely in Clojure
![Page 43: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/43.jpg)
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.
![Page 44: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/44.jpg)
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
![Page 45: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/45.jpg)
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
![Page 46: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/46.jpg)
Transactions
● Software Transactional Memory (STM)● (dosync ... )● STM uses Multiversion Concurrency Control
– marks the old data as obsolete and adds the newer version
– http://en.wikipedia.org/wiki/Multiversion_concurrency_control
![Page 47: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/47.jpg)
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 TX's.● all ref modifications succeed or none do● If TX2 commits a change while TX1 is working, it may
cause TX1 to be retried.
![Page 48: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/48.jpg)
Refs
commuteThis fn should be commutative, or, failing that, you must accept last-one-in-wins
behavior. commute allows for more concurrency than ref-set.
![Page 49: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/49.jpg)
Refs
![Page 50: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/50.jpg)
Atoms
swap!
reset!
compare-and-set! - sets atom to new value if and only if current value of the atom is identical to the old value.
![Page 51: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/51.jpg)
Agents
![Page 52: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/52.jpg)
Tooling
● Libraries● REPL programming● Leiningen
● project.clj
● No IDE required, but many choices
![Page 53: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/53.jpg)
Libraries
● Java libraries● Maven etc...
● Clojure libraries● clojars.org
Web: Ring, Noir, HTML: Hiccup, EnliveHadoop: Cascalog, Statistics: IncanterSQL: Korma, CQL, Riak: WelleOffice Documents: docjure& more...
![Page 54: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/54.jpg)
REPL via Leiningen] lein new clj-excel && cd clj-excel
now edit file “project.clj”
] lein deps && lein repl
![Page 55: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/55.jpg)
REPL via Leiningen] lein deps
] lein repl
![Page 56: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/56.jpg)
Reading an Excel file Excel file “sample.xlsx”
![Page 57: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/57.jpg)
Reading an Excel file
![Page 58: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/58.jpg)
IDE's and editors
● Any editor with syntax highlighting will do
● Your favorite Java IDE likely has a Clojure plugin
● What do most folks use?
![Page 59: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/59.jpg)
http://cemerick.com/2012/08/06/results-of-the-2012-state-of-clojure-survey/
1,372 responses were received over 7 days. Anounced via Twitter & Clojure mailing list (~6700 recipients)
![Page 60: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/60.jpg)
![Page 61: I know Java, why should I consider Clojure?](https://reader033.vdocuments.site/reader033/viewer/2022060108/554f60b6b4c905bb178b46ee/html5/thumbnails/61.jpg)
That was a whirlwind grand tour...
Thanks for listening!
(def email {:name “aaelony” :domain “@gmail.com”})