Clojure intro

51
(Clojure Intro) @basav

description

 

Transcript of Clojure intro

Page 1: Clojure intro

(Clojure Intro)@basav

Page 2: Clojure intro

Agenda

• Background

• What is Clojure?

• Why Clojure?

• Some code..

Page 3: Clojure intro

Clojure Vocabulary

Page 4: Clojure intro

Clojure Vocabulary

Functional Programming

LISP

recursion

STM

prefix notation

macrosJVM

homoiconicity S-expressionsREPL

Lazy Sequences

lambda calculus (λ)

Page 5: Clojure intro

Who are these guys?

Page 6: Clojure intro

Jedi Masters of

Page 8: Clojure intro

What is Clojure?

Clojure is a dynamic, strongly typed, functional, high performance implementation of Lisp on the JVM (and CLR and yes JavaScript)

Page 9: Clojure intro

What is Lisp?LISP stands for LISt Processing.

Designed by John McCarthy.

Implementation of lambda calculus.

Oldest Language - Circa 1958

Uses Lots of ( )s - Polish Prefix notation

Known for AI, DSLs etc

Page 10: Clojure intro

Example LISP Code

http://kazimirmajorinc.blogspot.in/2012/03/few-examples-of-lisp-code-typography.html

Page 11: Clojure intro

What is lambda calculus?• Formal system in Computer Science

for expressing computation by functions

• Consists of λ expressions

• variables v1, v2, ..., vn, ...

• the abstraction symbols λ and .

• parentheses ( )

• Examples

• λx.x — is a function taking an argument x, and returning x

• f x — is a function f applied to an argument x

Alonzo Church

Page 12: Clojure intro

Clojure History• Project led by Rich Hickey

• 1.0 in 2007

• 1.1 in 2009

• 1.2 in 2010

• 1.3 in 2011

• 1.4 in 2012

• 1.5 released in March 2013

• Maintained on Github

• contains one jar file clojure.jar

• You can get it from Maven repo

Page 13: Clojure intro

Why Clojure?

Page 15: Clojure intro

Q: How long have you been using Clojure?

Page 16: Clojure intro

Q: What language did you use just prior to adopting Clojure ?

Page 17: Clojure intro

Q: How would you characterize your use of Clojure today?

Page 18: Clojure intro

Q: In which domain(s) are you using Clojure?

Page 19: Clojure intro

Q: What have been the biggest wins for you in using Clojure?

Page 20: Clojure intro

Q: Which development environment(s) do you use to work with Clojure?

Page 21: Clojure intro

Why Clojure?• Good at working with Data

• Rich literal types like arrays, sets, data types

• Good at expressing algorithms

• Functional

• Great for concurrency

• Immutable data structure

• Innovative approach to state - STM

Page 22: Clojure intro

Some Clojure Syntax

Page 23: Clojure intro

println "Hello world!")( ;

Page 24: Clojure intro

println "Hello world!")( ;

Page 25: Clojure intro

Data literals

(1 2 3 "this" "is" :a :list) [1 2 3 "this is a vector"]

{:key "value" :two 2 }#{"I" "am" "set" }

Page 26: Clojure intro

(println "this is a function call")

Page 27: Clojure intro

(println "this is a function call")

fn call

Page 28: Clojure intro

(println "this is a function call")

fn call arguments

Page 29: Clojure intro

(println "this is a function call")

fn call

list

arguments

Page 30: Clojure intro

(fn-name arg1 arg2 arg3 ...)

syntax

Page 31: Clojure intro

Functional Programming

Page 32: Clojure intro

Functional ProgrammingA pure Function

A impure Function

Page 33: Clojure intro

Functional Programming

(Perform calculations, may call other pure

functions)

Arguments

Return Value

A pure Function

A impure Function

don’t...look outside their box...modify anything, anywhere ...print messages to the user...write to disk

i.e. No Side Effects

E.g. f(x) = x * x + 1

Page 34: Clojure intro

Functional Programming

(Perform calculations, may call other pure

functions)

(perform calculations)

Arguments

Return Value

Arguments

Return Value

Read External State

Write External State(Side Effect)

A pure Function

A impure Function

don’t...look outside their box...modify anything, anywhere ...print messages to the user...write to disk

i.e. No Side Effects

E.g. f(x) = x * x + 1

Page 35: Clojure intro

Functional Programming

Page 36: Clojure intro

Functional Programming

• Project Euler (http://projecteuler.net)

Page 37: Clojure intro

Functional Programming

• Project Euler (http://projecteuler.net)

• Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23.

Page 38: Clojure intro

Functional Programming

• Project Euler (http://projecteuler.net)

• Problem 1: If we list all the natural numbers below 10 that are multiples of 3 and 5, we get 3,5,6,9. The sum of these mutiples is 23.

Find the sum of all the multiples of 3 and 5 below 1000.

Page 39: Clojure intro

Project Euler: range

Page 40: Clojure intro

Project Euler: range

• I need a function to get a list of numbers

Page 41: Clojure intro

Project Euler: range

• I need a function to get a list of numbers

(range 5) => (0 1 2 3 4)

Page 42: Clojure intro

Project Euler: range

• I need a function to get a list of numbers

(range 5) => (0 1 2 3 4)

• So to get a range of (0-999) we can do

Page 43: Clojure intro

Project Euler: range

• I need a function to get a list of numbers

(range 5) => (0 1 2 3 4)

• So to get a range of (0-999) we can do

(range 1000)

Page 44: Clojure intro

Project Euler:numbers that are divisible by

3 and 5

• zero?

• rem

Page 45: Clojure intro

Project Euler: filter

• Next something that returns a list of selected values from a larger list

(filter even? (range 5)) => (0 2 4)

• filter function takes a predicate and a sequence (aka collection)

Page 46: Clojure intro

Project Euler: reduce

• Finally something to add up these numbers

• reduce takes a function and a sequence. Applies the function to the first two elements of the sequence, then applies the function to the result and third item and so on

(reduce + (range 6)) ==> 15

Page 47: Clojure intro

Project Euler:Completed program

(defn div_by_3_or_5? [n] (or (zero? (rem n 3)) (zero? (rem n 5)) ) )(reduce +

(filter div_by_3_or_5? (range 1000) ))

Page 48: Clojure intro

Books / Resourceshttp://alexott.net/en/clojure/video.html

Page 49: Clojure intro

Thank you

Page 50: Clojure intro

Appendix

Page 51: Clojure intro

(defn run [nvecs nitems nthreads niters] (let [vec-refs (vec (map (comp ref vec) (partition nitems (range (* nvecs nitems))))) swap #(let [v1 (rand-int nvecs) v2 (rand-int nvecs) i1 (rand-int nitems) i2 (rand-int nitems)] (dosync (let [temp (nth @(vec-refs v1) i1)] (alter (vec-refs v1) assoc i1 (nth @(vec-refs v2) i2)) (alter (vec-refs v2) assoc i2 temp)))) report #(do (prn (map deref vec-refs)) (println "Distinct:" (count (distinct (apply concat (map deref vec-refs))))))] (report) (dorun (apply pcalls (repeat nthreads #(dotimes [_ niters] (swap))))) (report))) (run 100 10 10 100000)

10 threads manipulating one shared data structure, which consists of 100 vectors each one containing 10 (initially sequential) unique numbers. Each thread then repeatedly selects two random positions in two random vectors and swaps them. All changes to the vectors occur in transactions by making use of clojure's software transactional memory system. That's why even after 100 000 iterations of each thread no number got lost.

Example of Clojure Code