This document provides a summary of an introduction to the Clojure programming language. It discusses what Clojure is, its timeline and adoption, functional programming concepts, concurrency features using Software Transactional Memory, the Lisp ideology it is based on including homoiconicity and its macro system. It also provides an overview of getting started with Clojure including using the REPL, basic syntax like symbols and keywords, data types, sequences, functions, and Java interoperability. Resources for learning more about Clojure are also listed.
What is Clojure GeneralPurpose Language Dynamic Functional Lisp Strengths Java/CLR/Javascript
7.
Clojure - Timeline Firstreleased October 16, 2007 1.0 released 05/04/09 1.1 released 12/31/09 1.2 released 08/19/10 1.3 released 09/23/11 ClojureScript released July, 2011
H/W Perf FreeLunch is over http://www.gotw.ca/publications/concurrency-ddj.htm
11.
Functional Programming No sideeffects – immutable Functions are first class citizens Functions passed in as parameters Functions return functions Functions can be created at runtime
12.
Software Transactional Memory Providestransactional feature for application in memory Implements Multi Value Concurrency Control (MVCC) Atomic, Consistent, and Isolated
LISP Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. - Eric Raymond, How to Become a Hacker
Atomic Data Types Clojure Example Java string “hello” String character a Character integer 10 Int/Long/BigInteger double 3.14159 Double double 3.14159M BigDecimal boolean true Boolean nil nil null symbol foo, + N/A keyword :foo, ::foo N/A
31.
Data Structures Type Example Characteristics list (2 4 6 8) • singly linked • grows at front vector [2 4 6 8] • indexed • grows at end map {:fname “Rich”, :lname • key/value pairs “Hickey”} • unique key set #{2 4 6 8} • unordered • unique keys
32.
Sequences An abstraction overtraditional lisp lists Provides a view into the lists Are lazy Some functions on sequences: (first seq): returns first element (cons elem seq): prepends element
33.
Syntax – (Prefix)Polish Notation The operator appears before the operands Format: (operator operand operand)
Essence vs Ceremony publicclass AddNumbers { public static void main(String args[]) { long sum = 0; for (int i = 1; i < 1001; i++) { sum += i; } System.out.println("The sum is: " + sum); } }
Useful Functions count: returnsnumber of items in a collection – (count *4 “hello” c+) => 3 apply: applies a function on the arguments – (apply – [10 5 2]) => 3
43.
Program Flow if, if-not – (if test consequent alternative) cond, condp – (cond & clauses) when, when-not – (when test & body)
44.
REPL: Doc Documentation canbe viewed with the doc macro user=> (doc +) ------------------------- clojure.core/+ ([] [x] [x y] [x y & more]) Returns the sum of nums. (+) returns 0. nil
45.
REPL: javadoc Opens abrowser with the javadoc for the requested class user=> (javadoc java.util.Random) "http://java.sun.com/javase/6/docs/api/java/uti l/Random.html"
46.
REPL: source Displays thesource for the function user=> (source even?) (defn even? "Returns true if n is even, throws an exception if n is not an integer" {:added "1.0"} [n] (zero? (bit-and n 1))) nil
Instantiate Java Class Format:(def instanceName (new ClassName)) Example: (def sdf (new SimpleDateFormat “yyyy-mm”)) Another way: (def sdf (SimpleDateFormat. “yyyy-mm”))
Calling Clojure fromJava public class ClojureGreeter { public static void main(String[] args) { RT.loadResourceScript("clojure_script.clj"); Var greet = RT.var("clj.sample", “say"); String result = (String) greet.invoke(“John"); System.out.println(“Greeting: " + result); } }
55.
Error Handling All exceptionsthrown are Runtime Exceptions. (try expr* catch-clause* finally-clause?) Example: (try (/ 1 0) (catch Exception e (println "in catch")) (finally (println "in finally")))
56.
MultiMethods Provides a generalindirect dispatching mechanism Dispatches based on type values, attributes, and metadata defmulti creates new multimethods defmethod create methods for defmulti