Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
Queue Data Structure
Queue Data Structure
Chargement dans…3
×

Consultez-les par la suite

1 sur 34 Publicité

Plus De Contenu Connexe

Diaporamas pour vous (20)

Similaire à Clojure (20)

Publicité

Plus récents (20)

Clojure

  1. 1. Demystifying (λ)Functional Programming with Clojure -Rohit Vaidya
  2. 2. Agenda ● Functional Programming ● Some Functional Jargon ● Understand Clojure ● Clojure Syntax ● Lein - Build tools for Clojure ● Meta Programming
  3. 3. Functional Programming ● Functional Programming takes a mathematical view of the world ● Nothing but elaboration of Lambda Calculus
  4. 4. λ- Calculus ● λ-Calculus is universal model of Computation ● Can be used to simulate a taped turing machine ● λ-Calculus treats functions anonymously ● Computable functions are fundamental to math and CS. ● λ-Calculus provides semantics for computation
  5. 5. λ- Calculus squaresum( x , y)→ x 2 +y 2 (x , y) → x 2 + y 2 (( x , y)→ x 2 +y 2 )(4,3) →4 2 +3 2 →25 (x →( y → x 2 +y 2 )(5))(2) ( y →5 2 +y 2 )(2) 5 2 +2 2 29
  6. 6. Jargon ● Homoiconic Language - Program structure is similar to its syntax ● Metaprogramming - Metaprogramming is the art of writing of computer programs with the ability to treat programs as their data ● Referential Transparency (Pure) - Always returns the same result for a given argument
  7. 7. Functional Programming-Clojure ● First class functions ● Immutable Data Structures ● Recursive looping ● Facilitates concurrency
  8. 8. Why Clojure? ● A Lisp – dynamic language ● Functional Programming ● Symbiotic with an established Platform ● Designed for Concurrency ● Embraces the JVM (Native to the JVM) ● Clojure is concise – Code as Data – (+ 3 2) This is a function call – '(+ 3 2) This is data
  9. 9. Why Clojure? ● You can tranlate data into function call at runtime ● Performs better than JavaScript, Ruby and Python ● Macros- Extending the Language – e.g: HTML templating is bloated – In java you mix HTML with Java or Java with HTML – What if your language knows generating HTML?
  10. 10. Why Clojure? – [:a {:href "http://github.com"} "GitHub"] – This converts to – <a href="http://github.com">GitHub</a> – The above can be done using Hiccup ● Has a REPL ● Define functions on the fly
  11. 11. Clojure Syntax ● ()[]{} Everything within () gets evaluated ● Almost no syntax rules ● Lisp Syntax (data = code).Code as Data!!! ● Lets write a simple function – Anonymous Function – Named function
  12. 12. Atomic Data Types ● Nil means nothing. Same as Java null ● Booleans true false ● Doubles 1.234 BigDecimals 10.123M ● Ratios 22/7 ● Strings “fred” Characters a b c ● Regex #”a*x”
  13. 13. Atomic Data ● Use clojure.core/class function to identify type of data
  14. 14. Clojure Functions ● Create a Clojure Function 1.Bind Name add to the function 2.Anonymous function with arguments x y 1.fn creates a anonymous function 3.Function description 4.Function body 1 2 31 4
  15. 15. Clojure Function ● Define function with macro form defn 1 2 3 1. Create a function add with x, y arguments 2. Function description 3. Function Body
  16. 16. Clojure Function Pure vs Impure ● Referential Transparency 1. Is pure. For certain x,y it will always return same result 2. Is impure. Getting a hike is dependent on side effect and not always deterministic 1 2
  17. 17. Special Forms ● Primitives build in clojure to perform core operations ● If do let fn loop recur etc are special forms ● (if true 1 2) – Returns 1 ● (let [x 3] println x) – Prints 3 – Scope restricted to let statment
  18. 18. Lists, Vectors, Sets and Maps ● Vectors – Similar to Array – 0 based collection – Syntax ● [1 2 3] ● (def abc [1 2 3]) ● (get abc 0)
  19. 19. Lists, Vectors, Sets and Maps ● Lists – Similar to Vectors – Cannot use get against lists – Syntax ● '(1 2 3) ● (nth '(1 2 3) 0) ● Lists to used if you want to add elements to the beginning – (conj '(1 2 3) 4) returns 4 1 2 3
  20. 20. Lists, Vectors, Sets and Maps ● Maps – Similars to dictionaries or hashes – Two types in Clojure ● HashMaps ● SortedMaps – Syntax: ● {:firstName “Rohit” :lastName “Vaidya”} ● (def hm {:a 1 :b 2}) ● (get hm :a) returns 1
  21. 21. Lists, Vectors, Sets and Maps ● HashSet – Sets are collection on unique values – Syntax ● (hash-set 1 2 3 3 2 1) – Create a hash set ● (contains? (hash-set 1 2 2 1 1) 1) – Check presense of a element – Returns true
  22. 22. Lists, Vectors, Sets and Maps ● HashSet – Sets are collection on unique values – Syntax ● (hash-set 1 2 3 3 2 1) – Create Hash Set ● (contains? (hash-set 1 2 2 1 1) 1) – Check presense of element – Returns true
  23. 23. Programming to Abstractions ● Sequence Abstraction, abstracts – List, – Vector – Set and Map ● Clojure defines map and reduce in terms of sequence abstractions and not any specific data structure ● y1 = f(x1), y2 = f(x2), . . . yn = f(x n).
  24. 24. Programming to Abstractions ● Visualizing mapping on a sequence abstraction
  25. 25. Programming to Abstractions 1. Define a increment function 2. Function description 3. Argument 4. Function Body 5. Map applied to a sequence(Vector). Map is a higher order function 1 2 3 4 5
  26. 26. Loops using Recursion 1. First function overload with zero arugment with arity 0 2. Second function overload with 1 argument i.e arity 1 3. Recursive call to by passing incremented value of number 1 2 3
  27. 27. Loops with Recursion 1 2 • Clojure equivalent of 1 in 2
  28. 28. Metaprogramming – Alchemy ● Reader: Is a clojure Parser – Converts text into clojure data structure ● Read String converts to list
  29. 29. Metaprogramming - Alchemy 1. Read a string and convert to list (data) 2. Read a String and evaluate it. Clojure expected operator. 3. Constructed prefix (reverse polish) with infix expression 4. Evaluated the constrcuted prefix expression 1 2 3 4
  30. 30. Metaprogramming - Alchemy ● Defmacro defines a macro ● Lesser verbose, metaprogramming 1. Define a macro 2. Argument for macro 3. Prefix to infix 4. Call to a macro 1 2 3 4
  31. 31. High Order Functions ● When a language takes a fn as arugment or returns fn as result ● A higher order function is – A function that takes function arguments – A function that retuns a function ● Some well known higher order functions – Map Reduce Remove Filter Iterate
  32. 32. Lein – the build tool ● Lein commands – lein new app clojure-noob – lein run – lein test ● Lein web app – lein new luminus my-app – cd my-app – lein run
  33. 33. References ● https://clojurebridge.github.io/community-docs/ ● http://www.slideshare.net/smartrevolution/how-a- ● http://www.braveclojure.com/ ● http://xahlee.info/clojure/clojure_index.html ● https://www.gnu.org/software/emacs/ ● https://mitpress.mit.edu/sicp/ ● Clojure by Rich Hickey ● 4Clojure
  34. 34. SICP

Notes de l'éditeur

  • Programs are functions that take certain values and produce certain values
  • Lambda calculus is formal system in mathematical logic for expressing computation using functional abstraction and application using variable substituion
  • Pair of x,y is mapped to x^2 + y^2
  • Ghost of John McCarthy
  • Programs are functions that take certain values and produce certain values
  • Create a anonymous function
    Create a name bound function defn macro
    (defn hike
    []
    (if (&amp;gt; (rand) 0.5)
    “You got hike”
    “Better luck next year”))
    (-&amp;gt; 2
    Inc
    Inc)
    ((fn [x y] (+ (* x x)(* y y)) 2 2)
  • Create a record and get a value from it
  • (defn increment
    [x]
    (inc x))
    (map increment [ 0 1 2 3 ])
    1 2 3 4
    (defn prefix
    [personalPronoun]
    (str PersonalPronoun “ Brave”))
    (map prefix [“I” “you” “she” “he”)

×