Clojure intro

325
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
325
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Clojure intro

  1. 1. (Clojure Intro) @basav
  2. 2. Agenda• Background• What is Clojure?• Why Clojure?• Some code..
  3. 3. Clojure Vocabulary
  4. 4. Clojure Vocabulary Functional LISP STM Programming lambda calculus recursion (λ) prefix notation JVMLazy Sequences macros homoiconicity REPL S-expressions
  5. 5. Who are these guys?
  6. 6. Jedi Masters of
  7. 7. Programming Taxonomy http://en.wikipedia.org/wiki/Comparison_of_programming_paradigms
  8. 8. What is Clojure?Clojure is a dynamic, stronglytyped, functional, highperformance implementationof Lisp on the JVM (and CLRand yes JavaScript)
  9. 9. What is Lisp?LISP stands for LISt Processing.Designed by John McCarthy.Implementation of lambdacalculus.Oldest Language - Circa 1958Uses Lots of ( )s - Polish PrefixnotationKnown for AI, DSLs etc
  10. 10. Example LISP Codehttp://kazimirmajorinc.blogspot.in/2012/03/few-examples-of-lisp-code-typography.html
  11. 11. What is lambda calculus?• Formal system computation by for expressing in Computer Science 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 Alonzo Church• f x — is a function f applied to an argument x
  12. 12. 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
  13. 13. Why Clojure?
  14. 14. http://java.dzone.com/articles/results-2012-state-clojure
  15. 15. Q: How long have you been using Clojure?
  16. 16. Q: What language did you use just prior to adopting Clojure ?
  17. 17. Q: How would you characterize your use ofClojure today?
  18. 18. Q: In which domain(s) are you using Clojure?
  19. 19. Q: What have been the biggest wins for you inusing Clojure?
  20. 20. Q: Which development environment(s) do youuse to work with Clojure?
  21. 21. 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
  22. 22. Some Clojure Syntax
  23. 23. println("Hello world!") ;
  24. 24. (println "Hello world!") ;
  25. 25. Data literals(1 2 3 "this" "is" :a :list)[1 2 3 "this is a vector"]{:key "value" :two 2 }#{"I" "am" "set" }
  26. 26. (println "this is a function call")
  27. 27. fn call(println "this is a function call")
  28. 28. fn call arguments(println "this is a function call")
  29. 29. fn call arguments(println "this is a function call") list
  30. 30. syntax(fn-name arg1 arg2 arg3 ...)
  31. 31. Functional Programming
  32. 32. Functional Programming A pure Function A impure Function
  33. 33. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to diskReturn Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function
  34. 34. Functional Programming A pure Function don’t ...look outside their box Arguments (Perform ...modify anything, anywhere ...print messages to the user calculations, may ...write to diskReturn Value call other pure i.e. No Side Effects functions) E.g. f(x) = x * x + 1 A impure Function Arguments Read External State (performReturn Value calculations) Write External State (Side Effect)
  35. 35. FunctionalProgramming
  36. 36. Functional Programming• Project Euler (http://projecteuler.net)
  37. 37. 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.
  38. 38. 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.
  39. 39. Project Euler: range
  40. 40. Project Euler: range• I need a function to get a list of numbers
  41. 41. Project Euler: range• I need a function to get a list of numbers (range 5) => (0 1 2 3 4)
  42. 42. 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
  43. 43. 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)
  44. 44. Project Euler:numbers that are divisible by 3 and 5• zero?• rem
  45. 45. 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)
  46. 46. 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
  47. 47. 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) ) )
  48. 48. Books / Resourceshttp://alexott.net/en/clojure/video.html
  49. 49. Thank you
  50. 50. Appendix
  51. 51. Example of Clojure Code10 threads manipulating one shared data structure, which consists of 100 vectors each onecontaining 10 (initially sequential) unique numbers. Each thread then repeatedly selects tworandom positions in two random vectors and swaps them. All changes to the vectors occurin transactions by making use of clojures software transactional memory system. Thatswhy even after 100 000 iterations of each thread no number got lost. (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)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×