Introduction to Clojure

35
Functional Programming on the JVM using Clojure Baishampayan Ghose Infinitely Beta Technologies GNUnify 2010 1 / 31

description

My Clojure talk at GNUnify 2010.

Transcript of Introduction to Clojure

Page 1: Introduction to Clojure

Functional Programming on the JVMusing Clojure

Baishampayan GhoseInfinitely Beta Technologies

GNUnify 2010

1 /31

Page 2: Introduction to Clojure

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

Page 3: Introduction to Clojure

Who am I?

My name is BG and I am a Computer guyFP headScale nerdProg-lang lawyerWeb standards ninja

FOSS geekStartup-ist

3 /31

Page 4: Introduction to Clojure

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

Page 5: Introduction to Clojure

What, why and how of Clojure

What is Clojure?Why a new programming language?How is Clojure any better than X?

5 /31

Page 6: Introduction to Clojure

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

Page 7: Introduction to Clojure

Fundamentals

Clojure is a LispClojure is dynamicClojure is hosted on the JVMClojure is functionalClojure is geared towards concurrencyFree & Open Source

7 /31

Page 8: Introduction to Clojure

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

Page 9: Introduction to Clojure

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

Page 10: Introduction to Clojure

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

Page 11: Introduction to Clojure

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

Page 12: Introduction to Clojure

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

Page 13: Introduction to Clojure

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

Page 14: Introduction to Clojure

Hello, Clojure!

hello.clj(ns hello)

(defn hello"Say hello to name"[name](str "Hello, " name "!"))

(hello "Clojure") ; -> "Hello, Clojure!"

13 /31

Page 15: Introduction to Clojure

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

Page 16: Introduction to Clojure

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

Page 17: Introduction to Clojure

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

Page 18: Introduction to Clojure

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

Page 19: Introduction to Clojure

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

Page 20: Introduction to Clojure

Concurrency

Simultaneous executionAvoid reading,yielding inconsistent data

Synchronous AsynchronousCoordinated refIndependent atom agentUnshared var

Table: Building blocks of Clojure concurrency

19 /31

Page 21: Introduction to Clojure

Concurrency

Simultaneous executionAvoid reading,yielding inconsistent data

Synchronous AsynchronousCoordinated refIndependent atom agentUnshared var

Table: Building blocks of Clojure concurrency

19 /31

Page 22: Introduction to Clojure

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

Page 23: Introduction to Clojure

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

Page 24: Introduction to Clojure

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

Page 25: Introduction to Clojure

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

Page 26: Introduction to Clojure

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

Page 27: Introduction to Clojure

There is more!

MetadataDestructuringTransientsZippersFutures, Promisesclojure.contrib

Ping me after the talk for details

23 /31

Page 28: Introduction to Clojure

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

Page 29: Introduction to Clojure

Tools

Emacs + SLIME + pareditViMClojureEnclojure + IntelliJ IDEACounterclockwise + EclipseLeiningen

25 /31

Page 30: Introduction to Clojure

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

Page 31: Introduction to Clojure

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

Page 32: Introduction to Clojure

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

Page 33: Introduction to Clojure

Questions?

29 /31

Page 34: Introduction to Clojure

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

Page 35: Introduction to Clojure

Contacting me

http://freegeek.in/

[email protected]

@ghoseb on TwitterSlides on - http://bit.ly/gnunify-clojure

Infinitely Beta is recruiting smart hackers!http://infinitelybeta.com/jobs/

CC BY SA

31 /31