SlideShare a Scribd company logo
1 of 34
Download to read 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

More Related Content

What's hot

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
 

What's hot (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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

Editor's Notes

  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”)