Clojure is a general-purpose, functional programming language that runs on the Java Virtual Machine and emphasizes simplicity and concurrency. It was created by Rich Hickey and is known for its immutable data structures, code-as-data (homoiconicity), and interoperability with Java libraries. Clojure is used in industries by companies like CapitalOne, Amazon, and Facebook, and its developers report high satisfaction levels.
90 Second Overview….. Clojureis….. - A dialect of Lisp - General purpose language - Emphasizes functional programming - Runs on the Java Virtual Machine - Designed for Concurrency - Used in industry -- CapitalOne, Amazon, Facebook, Oracle
Simplicity “We suffer fromso much incidental complexity in traditional OO languages, both syntactic and semantic, that I don’t think we even realise it anymore. I wanted to make ‘doing the right thing’ not a matter of convention and discipline, but the default. I wanted a solid concurrency story and great interoperability with existing Java libraries,.” -Rich Hickey, creator of Clojure Rich Hickey, Clojure, Benevolent Dictator for Life CLOJURE: - First Available: 2007 - Latest Stable Release: 1.8 January 19, 2016
Why JVM? ● Familiar- run just like Java apps ● Use some familiar Java objects ● Many available libraries ● Established track record ● Now open-sourced. http://www.braveclojure.com/java/ (Chapter 12) clojure.org/about/rationale
9.
What are Clojure’sCharacteristics? ● Functional - succinct, understandable, reusable ● Defaults to Immutability ○ Simplifies reasoning and testing ○ Easier to share objects ○ Thread safe ● but allows mutability! ● Code-as-Data (Homoiconicity) ● Syntactic Abstraction Immutable objects are always thread safe. —Brian Goetz, Java Concurrency in Practice The Joy of Clojure Chapter 6.
10.
Examples Homoiconicity Code written inthe language is encoded as data structures that the language has tools to manipulate Syntatic Abstraction Permits the definition of new language constructs whose meaning can be understood in terms of static translation into the core language.As Data: As Code.
Truthiness What is truthy? ●true ● 1 ● (= 1 1) ● 0 ● “False” ● [] ● Literally any value but false or nil What is falsey? ● false ● nil ● (= 3 1) Nil Punning: Handling Clojure’s empty sequences that are Truthy Every[thing] is "true" all the time, unless it is nil or false. —Brian Goetz, The Joy of Clojure
14.
Clojure Data Structures ●They are immutable by default ● Support metadata ● Implement java.lang.Iterable ● Implement the read-only part of java.util.Collection http://clojure.org/reference/data_structures
15.
Data Collection Types- Part 1 Lists Singly linked lists First item in calling position Heterogeneous elements Compare to Haskell: Homogeneous lists Vectors Simply evaluate each item in order. Quick look-up times Heterogeneous elements No calling position
16.
Data Collection Types- Part 2 Maps Maps store unique keys and one value per key (dictionaries and hashes) Two types: 1. Hashed: key has hashCode, equals 2. Sorted: implement Comparable Sets Store zero or more unique items
17.
Some Mutable DataStructures! Atoms Managed shared synchronous independent state Refs Transaction references Safe shared use of mutable storage Require SW transactional memory system Agents Independent, asynchronous change of individual locations Only allow change due to an action Var Mutable storage location Can be dynamically rebound per thread Ensure safety through thread isolation
Functions First Class Created ondemand Stored in a data structure Passed as an argument Returned as a value Higher-Order Takes one or more function args Returns function as result Pure Functions ALWAYS return same result No observable side effects (defn [arg1 arg2] (;function code )) def or defn? ● Both bind a symbol or name ● def is only evaluated once ● defn is evaluated every time it is called
Concurrency Doseq Do over asequence Dosync Do all or nothing Future Generate a thread Delay Bind function but don’t evaluate Promise Create a binding with no value
Clojure Shortcomings: ● Dynamictype checking ● Doesn’t scale well: code size, team size, time elapsed ● Startup time ● If JVM is not a good solution, then Clojure isn’t either ○ Systems programming limitations ○ Real-Time http://martintrojer.github.io/beyond-clojure/2016/04/19/beyond-clojure-prelude https://www.quora.com/What-is-clojure-bad-at
Clojure in Industry TheBoeing 737 MAX Onboard Maintenance Function is 32,000 lines of Clojure. First time Clojure used in aircraft software. One of the largest code bases to date.
34.
When to chooseClojure? Handle large amounts of data with significant hardware limitations. When you want more concise code, easier to test and debug. Easier to get over 90% coverage with unit testing. When JVM or CLR can be used.
35.
Clojure developers arethe happiest developers From an analysis of Reddit comments, http://www.itworld.com/article/2693998/big-data/clojure- developers-are-the-happiest-developers.html
Further Info The Joyof Clojure Book by Chris Houser and Michael Fogus Clojure Programming Book by By Chas Emerick, Brian Carper, Christophe Grand Simple Made Easy Video by Rich Hickey