Upload
basav-n
View
367
Download
5
Embed Size (px)
DESCRIPTION
Citation preview
(Clojure Intro)@basav
Agenda
• Background
• What is Clojure?
• Why Clojure?
• Some code..
Clojure Vocabulary
Clojure Vocabulary
Functional Programming
LISP
recursion
STM
prefix notation
macrosJVM
homoiconicity S-expressionsREPL
Lazy Sequences
lambda calculus (λ)
Who are these guys?
Jedi Masters of
Programming Taxonomyhttp://en.wikipedia.org/wiki/
Comparison_of_programming_paradigms
What is Clojure?
Clojure is a dynamic, strongly typed, functional, high performance implementation of Lisp on the JVM (and CLR and yes JavaScript)
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
Example LISP Code
http://kazimirmajorinc.blogspot.in/2012/03/few-examples-of-lisp-code-typography.html
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
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
Why Clojure?
http://java.dzone.com/articles/results-2012-state-clojure
Q: How long have you been using Clojure?
Q: What language did you use just prior to adopting Clojure ?
Q: How would you characterize your use of Clojure today?
Q: In which domain(s) are you using Clojure?
Q: What have been the biggest wins for you in using Clojure?
Q: Which development environment(s) do you use to work with Clojure?
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
Some Clojure Syntax
println "Hello world!")( ;
println "Hello world!")( ;
Data literals
(1 2 3 "this" "is" :a :list) [1 2 3 "this is a vector"]
{:key "value" :two 2 }#{"I" "am" "set" }
(println "this is a function call")
(println "this is a function call")
fn call
(println "this is a function call")
fn call arguments
(println "this is a function call")
fn call
list
arguments
(fn-name arg1 arg2 arg3 ...)
syntax
Functional Programming
Functional ProgrammingA pure Function
A impure Function
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
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
Functional Programming
Functional Programming
• Project Euler (http://projecteuler.net)
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.
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.
Project Euler: range
Project Euler: range
• I need a function to get a list of numbers
Project Euler: range
• I need a function to get a list of numbers
(range 5) => (0 1 2 3 4)
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
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)
Project Euler:numbers that are divisible by
3 and 5
• zero?
• rem
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)
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
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) ))
Books / Resourceshttp://alexott.net/en/clojure/video.html
Thank you
Appendix
(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