SlideShare uma empresa Scribd logo
1 de 34
Baixar para ler offline
Demystifying (λ)Functional Programming with
Clojure
-Rohit Vaidya
Agenda
● Functional Programming
● Some Functional Jargon
● Understand Clojure
● Clojure Syntax
● Lein - Build tools for Clojure
● Meta Programming
Functional Programming
● Functional Programming takes a
mathematical view of the world
● Nothing but elaboration of Lambda
Calculus
λ- 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
λ- 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
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
Functional Programming-Clojure
● First class functions
● Immutable Data Structures
● Recursive looping
● Facilitates concurrency
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
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?
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
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
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”
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 creates a anonymous function
3.Function description
4.Function body
1
2
31
4
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
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
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
Lists, Vectors, Sets and Maps
● Vectors
– Similar to Array
– 0 based collection
– Syntax
● [1 2 3]
● (def abc [1 2 3])
● (get abc 0)
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
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
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
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
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).
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 applied to a sequence(Vector). Map is a higher order function
1
2
3
4
5
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
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 to list
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
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
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
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
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
SICP

Mais conteúdo relacionado

Mais procurados

C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning ObjectsJay Patel
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java DevelopersMartin Ockajak
 
Refinement Types for Haskell
Refinement Types for HaskellRefinement Types for Haskell
Refinement Types for HaskellMartin Ockajak
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & ScalaMartin Ockajak
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data scienceJohn Cant
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAdelina Ahadova
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingTiểu Hổ
 
Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM Tikal Knowledge
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetDConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetAndrei Alexandrescu
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core ConceptPin-Lun Huang
 
Day 3 examples u6f13
Day 3 examples u6f13Day 3 examples u6f13
Day 3 examples u6f13jchartiersjsd
 

Mais procurados (20)

C++ Returning Objects
C++ Returning ObjectsC++ Returning Objects
C++ Returning Objects
 
Scala for Java Developers
Scala for Java DevelopersScala for Java Developers
Scala for Java Developers
 
Nikit
NikitNikit
Nikit
 
Refinement Types for Haskell
Refinement Types for HaskellRefinement Types for Haskell
Refinement Types for Haskell
 
Comparing Haskell & Scala
Comparing Haskell & ScalaComparing Haskell & Scala
Comparing Haskell & Scala
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Algorithm Complexity and Main Concepts
Algorithm Complexity and Main ConceptsAlgorithm Complexity and Main Concepts
Algorithm Complexity and Main Concepts
 
binary search
binary searchbinary search
binary search
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 
Fluent14
Fluent14Fluent14
Fluent14
 
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & SearchingHub 102 - Lesson 5 - Algorithm: Sorting & Searching
Hub 102 - Lesson 5 - Algorithm: Sorting & Searching
 
Merge sort
Merge sortMerge sort
Merge sort
 
Big o notation
Big o notationBig o notation
Big o notation
 
Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM
 
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury SechetDConf 2016: Bitpacking Like a Madman by Amaury Sechet
DConf 2016: Bitpacking Like a Madman by Amaury Sechet
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Java & OOP Core Concept
Java & OOP Core ConceptJava & OOP Core Concept
Java & OOP Core Concept
 
Computer Network Assignment Help
Computer Network Assignment HelpComputer Network Assignment Help
Computer Network Assignment Help
 
Day 3 examples u6f13
Day 3 examples u6f13Day 3 examples u6f13
Day 3 examples u6f13
 

Semelhante a Clojure

Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptxKarthickT28
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with ClojureCarlo Sciolla
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojurePaul Lam
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data ManagementAlbert Bifet
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programmingSteve Zhang
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsMiles Sabin
 
Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageAsankhaya Sharma
 

Semelhante a Clojure (20)

Clojure intro
Clojure introClojure intro
Clojure intro
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Scala qq
Scala qqScala qq
Scala qq
 
Java 8
Java 8Java 8
Java 8
 
Functional Programming.pptx
Functional Programming.pptxFunctional Programming.pptx
Functional Programming.pptx
 
What`s New in Java 8
What`s New in Java 8What`s New in Java 8
What`s New in Java 8
 
Functional Programming with Clojure
Functional Programming with ClojureFunctional Programming with Clojure
Functional Programming with Clojure
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
A gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojureA gentle introduction to functional programming through music and clojure
A gentle introduction to functional programming through music and clojure
 
06. haskell type builder
06. haskell type builder06. haskell type builder
06. haskell type builder
 
Real Time Big Data Management
Real Time Big Data ManagementReal Time Big Data Management
Real Time Big Data Management
 
The joy of functional programming
The joy of functional programmingThe joy of functional programming
The joy of functional programming
 
Neo4j: Graph-like power
Neo4j: Graph-like powerNeo4j: Graph-like power
Neo4j: Graph-like power
 
An Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional ParadigmsAn Introduction to Scala - Blending OO and Functional Paradigms
An Introduction to Scala - Blending OO and Functional Paradigms
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
Design and Implementation of the Security Graph Language
Design and Implementation of the Security Graph LanguageDesign and Implementation of the Security Graph Language
Design and Implementation of the Security Graph Language
 
Elixir basics
Elixir basicsElixir basics
Elixir basics
 
Scheme 核心概念(一)
Scheme 核心概念(一)Scheme 核心概念(一)
Scheme 核心概念(一)
 
R programmingmilano
R programmingmilanoR programmingmilano
R programmingmilano
 

Último

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 

Último (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 

Clojure

  • 1. Demystifying (λ)Functional Programming with Clojure -Rohit Vaidya
  • 2. Agenda ● Functional Programming ● Some Functional Jargon ● Understand Clojure ● Clojure Syntax ● Lein - Build tools for Clojure ● Meta Programming
  • 3. Functional Programming ● Functional Programming takes a mathematical view of the world ● Nothing but elaboration of Lambda Calculus
  • 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. λ- 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. 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. Functional Programming-Clojure ● First class functions ● Immutable Data Structures ● Recursive looping ● Facilitates concurrency
  • 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. 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. 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. 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. 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. Atomic Data ● Use clojure.core/class function to identify type of data
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Programming to Abstractions ● Visualizing mapping on a sequence abstraction
  • 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. 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. Loops with Recursion 1 2 • Clojure equivalent of 1 in 2
  • 28. Metaprogramming – Alchemy ● Reader: Is a clojure Parser – Converts text into clojure data structure ● Read String converts to list
  • 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. 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. 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. 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. 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. SICP

Notas do Editor

  1. Programs are functions that take certain values and produce certain values
  2. Lambda calculus is formal system in mathematical logic for expressing computation using functional abstraction and application using variable substituion
  3. Pair of x,y is mapped to x^2 + y^2
  4. Ghost of John McCarthy
  5. Programs are functions that take certain values and produce certain values
  6. 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)
  7. Create a record and get a value from it
  8. (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”)