Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Demystifying (λ)Functional Programming with
Clojure
-Rohit Vaidya
Agenda
● Functional Programming
● Some Functional Jargon
● Understand Clojure
● Clojure Syntax
● Lein - Build tools for Cl...
Functional Programming
● Functional Programming takes a
mathematical view of the world
● Nothing but elaboration of Lambda...
λ- Calculus
● λ-Calculus is universal model of
Computation
● Can be used to simulate a taped turing
machine
● λ-Calculus t...
λ- 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
)(...
Jargon
● Homoiconic Language - Program
structure is similar to its syntax
● Metaprogramming - Metaprogramming is
the art o...
Functional Programming-Clojure
● First class functions
● Immutable Data Structures
● Recursive looping
● Facilitates concu...
Why Clojure?
● A Lisp – dynamic language
● Functional Programming
● Symbiotic with an established Platform
● Designed for ...
Why Clojure?
● You can tranlate data into function call at
runtime
● Performs better than JavaScript, Ruby and
Python
● Ma...
Why Clojure?
– [:a {:href "http://github.com"} "GitHub"]
– This converts to
– <a href="http://github.com">GitHub</a>
– The...
Clojure Syntax
● ()[]{} Everything within () gets evaluated
● Almost no syntax rules
● Lisp Syntax (data = code).Code as D...
Atomic Data Types
● Nil means nothing. Same as Java null
● Booleans true false
● Doubles 1.234 BigDecimals 10.123M
● Ratio...
Atomic Data
● Use clojure.core/class function to identify
type of data
Clojure Functions
● Create a Clojure Function
1.Bind Name add to the function
2.Anonymous function with arguments x y
1.fn...
Clojure Function
● Define function with macro form defn
1
2
3
1. Create a function add with x, y
arguments
2. Function des...
Clojure Function Pure vs Impure
● Referential Transparency
1. Is pure. For certain x,y it will always return
same result
2...
Special Forms
● Primitives build in clojure to perform core
operations
● If do let fn loop recur etc are special forms
● (...
Lists, Vectors, Sets and Maps
● Vectors
– Similar to Array
– 0 based collection
– Syntax
● [1 2 3]
● (def abc [1 2 3])
● (...
Lists, Vectors, Sets and Maps
● Lists
– Similar to Vectors
– Cannot use get against lists
– Syntax
● '(1 2 3)
● (nth '(1 2...
Lists, Vectors, Sets and Maps
● Maps
– Similars to dictionaries or hashes
– Two types in Clojure
● HashMaps
● SortedMaps
–...
Lists, Vectors, Sets and Maps
● HashSet
– Sets are collection on unique values
– Syntax
● (hash-set 1 2 3 3 2 1)
– Create ...
Lists, Vectors, Sets and Maps
● HashSet
– Sets are collection on unique values
– Syntax
● (hash-set 1 2 3 3 2 1)
– Create ...
Programming to Abstractions
● Sequence Abstraction, abstracts
– List,
– Vector
– Set and Map
● Clojure defines map and red...
Programming to Abstractions
● Visualizing mapping on a sequence abstraction
Programming to Abstractions
1. Define a increment function
2. Function description
3. Argument
4. Function Body
5. Map app...
Loops using Recursion
1. First function overload
with zero arugment with
arity 0
2. Second function overload
with 1 argume...
Loops with Recursion
1
2
• Clojure equivalent of 1 in 2
Metaprogramming – Alchemy
● Reader: Is a clojure Parser
– Converts text into clojure data structure
● Read String converts...
Metaprogramming - Alchemy
1. Read a string and convert to list (data)
2. Read a String and evaluate it. Clojure expected o...
Metaprogramming - Alchemy
● Defmacro defines a
macro
● Lesser verbose,
metaprogramming
1. Define a macro
2. Argument for m...
High Order Functions
● When a language takes a fn as arugment
or returns fn as result
● A higher order function is
– A fun...
Lein – the build tool
● Lein commands
– lein new app clojure-noob
– lein run
– lein test
● Lein web app
– lein new luminus...
References
● https://clojurebridge.github.io/community-docs/
● http://www.slideshare.net/smartrevolution/how-a-
● http://w...
SICP
Upcoming SlideShare
Loading in …5
×

Clojure

3,852 views

Published on

Presentation that goes over basics of functional programming. Discussed basic Clojure syntax and concepts like MetaProgramming.

Published in: Technology
  • Sex in your area is here: ❶❶❶ http://bit.ly/2F4cEJi ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Follow the link, new dating source: ❤❤❤ http://bit.ly/2F4cEJi ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

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

×