Functional Programming on the JVMusing Clojure
Baishampayan GhoseInfinitely Beta Technologies
GNUnify 2010
1 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
2 /31
Who am I?
My name is BG and I am a Computer guyFP headScale nerdProg-lang lawyerWeb standards ninja
FOSS geekStartup-ist
3 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
4 /31
What, why and how of Clojure
What is Clojure?Why a new programming language?How is Clojure any better than X?
5 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
6 /31
Fundamentals
Clojure is a LispClojure is dynamicClojure is hosted on the JVMClojure is functionalClojure is geared towards concurrencyFree & Open Source
7 /31
Integers — 1234567891234
Doubles — 3.14, BigDecimals — 1.23M
Ratios — 22/4
Strings — “foo”, Characters — \a \b \cSymbols — foo bar, Keyword — :foo :bar
Booleans — true false, Null — nil
Regex patterns — #“[a-zA-Z0-9]”
8 /31
Data Structures
Lists(1 2 3 4 5), (foo bar baz), (list 1 2 3)
Vectors[1 2 3 4 5], [foo bar baz], (vector 1 2 3)
Maps{:a 1 :b 2 :c 3}, (hash-map :x 1 :y 2)
Sets#{foo bar baz}
9 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
10 /31
Syntax
There is no other syntax!Data structures are the codeNo other text based syntax, just differentinterpretationsThere is a fancy name for this — Homoiconicity
Everything is an expressionAll data literals stand for themselves, except —
SymbolsLists
11 /31
Syntax
There is no other syntax!Data structures are the codeNo other text based syntax, just differentinterpretationsThere is a fancy name for this — HomoiconicityEverything is an expressionAll data literals stand for themselves, except —
SymbolsLists
11 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
12 /31
Hello, Clojure!
hello.clj(ns hello)
(defn hello"Say hello to name"[name](str "Hello, " name "!"))
(hello "Clojure") ; -> "Hello, Clojure!"
13 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
14 /31
Sequences
An abstraction over traditional Lisp listsProvides an uniform way of walking through datastructures(seq coll)
If collection is non-empty, return a sequence object(first s)
Return the first item(rest s)
Return a seq of the rest of elements
15 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
16 /31
Java Inter-op
Wrapper free interface to JavaSyntactic sugar to make Java invocation easyCore Clojure abstractions are Java interfacesClojure functions implement Callable & RunnableClojure sequence library works on Java iterablesEasy to implement, extend Java interfaces (ifneeded)Almost identical to Java in terms of speed
17 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
18 /31
Concurrency
Simultaneous executionAvoid reading,yielding inconsistent data
Synchronous AsynchronousCoordinated refIndependent atom agentUnshared var
Table: Building blocks of Clojure concurrency
19 /31
Concurrency
Simultaneous executionAvoid reading,yielding inconsistent data
Synchronous AsynchronousCoordinated refIndependent atom agentUnshared var
Table: Building blocks of Clojure concurrency
19 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
20 /31
Multimethods
Generalised indirect dispatchDispatch on a arbitrary function of the arguments
Call sequenceCall dispatch function on args to get dispatch valueFind method associated with dispatch value
Else call default method, else error
21 /31
Multimethods
Generalised indirect dispatchDispatch on a arbitrary function of the argumentsCall sequence
Call dispatch function on args to get dispatch valueFind method associated with dispatch value
Else call default method, else error
21 /31
Multimethods
Generalised indirect dispatchDispatch on a arbitrary function of the argumentsCall sequence
Call dispatch function on args to get dispatch valueFind method associated with dispatch value
Else call default method, else error
21 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
22 /31
There is more!
MetadataDestructuringTransientsZippersFutures, Promisesclojure.contrib
Ping me after the talk for details
23 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
24 /31
Tools
Emacs + SLIME + pareditViMClojureEnclojure + IntelliJ IDEACounterclockwise + EclipseLeiningen
25 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
26 /31
Resources
Programming Clojure by Stuart Hallowayhttp://en.wikibooks.org/wiki/Clojure
http://clojure.org
http://groups.google.com/group/clojure
http://planet.clojure.in
http://clojure.blip.tv
27 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
28 /31
Questions?
29 /31
Outline1 Who am I?2 What, why and how of Clojure3 Fundamentals4 Syntax5 Hello, Clojure!6 Sequences7 Java Inter-op8 Concurrency9 Multimethods10 There is more!11 Tools12 Resources13 Q & A14 Contacting me
30 /31
Contacting me
http://freegeek.in/
@ghoseb on TwitterSlides on - http://bit.ly/gnunify-clojure
Infinitely Beta is recruiting smart hackers!http://infinitelybeta.com/jobs/
CC BY SA
31 /31
Top Related