Functional Programming
with
Lucy Fang
@crushoncode
Imperative
OOP
Declarative
Functional
Programming paradigms
A comparison...
Object-oriented Functional
The HOW
How to perform tasks and how to track changes
in state
The WHAT
What information is desired and what
transformations are required
State changes are important Immutable state and data structures
Loops, conditionals, and method calls
Order of execution is important.
Function calls and managing loops through
recursion.
Instances of structures or classes Functions as first-class objects and data
collections.
Functional programming with clojure
What is Clojure?
● Dialect of the Lisp programming language
(+ 1 2)
● Hosted language running on the JVM (java virtual machine)
● Interoperable with the host language
● Written like a data structure
Functional programming with clojure
Basic Clojure Syntax
Define a value or data e.g. (def name “value”)
Define a function e.g. (defn functionName [argument] (behavior))
Binding a value e.g. (let [value])
Collections:
(list 1 2 3) => ‘(1 2 3)
(vec ‘(1 2 3) => [1 2 3] //similar to an array, zero indexed
(set ‘(1 2 1) => #{1 2} // set of unique values
(map + [1 2 3] [4 5 6]) => (5 7 9)
First-class functions
● Treats functions as values so you can assign it to a variable, pass it to other
functions, store it in data structures e.g arrays, lists
● Can be used in higher-order functions - takes functions as arguments or
returns a function as a result
● Constructed at runtime
Why is this useful?
● When your inputs are yet to be provided
● reduce unnecessary code
● Increase reusability - keep it DRY
First-class functions and higher order functions
(first class function)
=> (def double-it (number)
(fn [number] (* 2 number))
(higher-order function)
=> (map double-it [1 2 3])
=> (2 4 6)
(def double-it (partial * 2))
same as
(fn [number] (* 2 number)) or
#(* 2 %)
Function Composition
Takes a set of functions and returns a function that is the composition of those
functions.
(filter (comp not zero?) [0 1 0 2 0 3 0 4])
=> (1 2 3 4)
Functional programming with clojure
Pure function?
(def age 20)
(defn whats-my-age [name]
(str name “: ” age))
=> (whats-my-age “Prashant”)
=> “Prashant: 20”
Impure Pure
(def age 20)
(defn my-age [name]
(str name “:” age))
=> (my-age “Prashant”)
=> “Prashant:20”
Create another age...
(defn my-age [name]
(let [age 20]
(str name “:” age)))
=> (my-age “Prashant”)
=> “Prashant:20”
Libraries
(:import java.util.Date)
(defn race-complete [racer-name]
(str race-name “ finished on "(java.util.Date.))
=> (race-complete “Prashant")
=> "Prashant finished on Sun Oct 01 10:03:59 BST 2017"
=> (race-complete “Prashant")
=> "Prashant finished on Sun Oct 01 10:04:48 BST 2017"
Almost a lottery scam!
(defn scratchie-lottery []
(if (= (rand) 3)
"Congrats! You’re a millionaire!"
"Hahaha your money is mine!"))
=> (scratchie-lottery)
=> "Hahaha your money is mine!"
Pure doesn’t always mean Idempotent
A pure function
- calling f(x) will give the same result no matter how many times you call
it. E.g. (defn add-1 [n] (+ 1 n)
An idempotent function
f(f(x)) is the same as f(x).
E.g.
(clojure.string/upper-case (clojure.string/upper-case “hello”)) => “HELLO”
(float (float 2))
Why are pure functions useful?
● when given the same input, will always return the same output and does not
have any observable side effect - you have CERTAINTY!
● makes testing much easier - we have less set up as we simply give it the
input and assert on the output e.g. no need for a payment gateway
● they can be run anywhere, when do you find you are copying the code over?
A value is something that doesn’t change.
20 is 20.
1 January 2017 is just that.
$5 doesn’t become $10.
My favorite fruits are bananas and cherries
(hmmmm….but not together).
What if my favorite fruit is now figs?
Well it will be a different set.
An identity with a state whose value is a value
at a point in time.
Functional programming with clojure
Immutability - Persistent data structures
(list 1 2 3) => ‘(1 2 3)
(vec ‘(1 2 3) => [1 2 3] //similar to an array, zero indexed
(set ‘(1 2 1) => #{1 2} // set of unique values
(map + [1 2 3] [4 5 6]) => (5 7 9)
Memory is shared
So what if we do need to change state?
● Memory state can be shared readily between threads which makes
concurrency easier
● Software transactional memory (STM) supports sharing of changing state
between threads in a synchronous and coordinated manner.
● Vars are for when you need to store something on a per-thread basis. If you
have a multi-threaded program and each thread needs its own private state,
put that state in a var.
Coordinated Uncoordinated
synchronous Ref (STM) atoms
asynchronous - agents
Recursion vs tail recursion
Traditional recursive
(defn factorial [n]
(if (zero? n)
1
(* n (factorial (dec n)))))
You don’t get the result of your
calculation until you have returned from
every recursive call. Generates a new
stackframe for each call.
Tail recursion
(defn factorial [n]
(loop [count n
result 1]
(if (pos? count)
(recur (dec count) (* result count))
result )))
Processing occurs before recursive call.
Reuse existing stackframes.
Compare on the call stack
Traditional recursive Tail recursion
(factorial 5) : 5 * _
(factorial 4) : 4 * _
(factorial 3) : 3 * _
(factorial 2) : 2 * _
(factorial 1) : 1 * _
(factorial 0) : 1
(factorial 5)
1
1
2
6
24
(factorial 5) = 120 (factorial 5) =
(loop 5 0)(loop 4 5)(loop 3 20)(loop 2 60)(loop 1 120)
120
count
n
Lazy evaluation
● Only return a value when necessary “call-by-need”
● Things are calculated not when they are defined, but when they are actually
needed.
● Maintain precision
● Optimise evaluation
Examples: ratios, lazy sequence such as range and repeat
TDD using midje
● Test driven development - Red/Green/Refactor
● Clean, tested, lean
● Midje - testing framework for clojure
Clojure resources
- Lots of resources
- Clojure documentation
- https://www.braveclojure.com/
- https://clojure.org/community/books
- Clojure koans
- tryclj.com/ online REPL
- https://clojure.org/api/cheatsheet
(def time-to “code!”)

More Related Content

PPTX
Functions & Recursion
PDF
The Ring programming language version 1.5.1 book - Part 19 of 180
PDF
The Ring programming language version 1.2 book - Part 9 of 84
PDF
The Ring programming language version 1.2 book - Part 11 of 84
PPTX
20170317 functional programming in julia
PDF
The Ring programming language version 1.2 book - Part 10 of 84
PDF
The Ring programming language version 1.2 book - Part 12 of 84
PDF
The Ring programming language version 1.8 book - Part 37 of 202
Functions & Recursion
The Ring programming language version 1.5.1 book - Part 19 of 180
The Ring programming language version 1.2 book - Part 9 of 84
The Ring programming language version 1.2 book - Part 11 of 84
20170317 functional programming in julia
The Ring programming language version 1.2 book - Part 10 of 84
The Ring programming language version 1.2 book - Part 12 of 84
The Ring programming language version 1.8 book - Part 37 of 202

What's hot (20)

PPTX
Keywords of java
PDF
JavaProgrammingManual
PDF
The Ring programming language version 1.8 book - Part 25 of 202
PDF
The what over the how (another way on android development with kotlin)
PPTX
Operator overload rr
PDF
The Ring programming language version 1.5.1 book - Part 30 of 180
PDF
The Ring programming language version 1.9 book - Part 27 of 210
PDF
Python lecture 05
PDF
TeraSort
PPTX
Image Recognition with Neural Network
PDF
Swift Tutorial 2
TXT
123
PDF
Clojure functions examples
PDF
The Ring programming language version 1.5.2 book - Part 31 of 181
PDF
The Ring programming language version 1.2 book - Part 21 of 84
PDF
The Ring programming language version 1.7 book - Part 35 of 196
PDF
Quick python reference
PPTX
R programming
PDF
7 Habits For a More Functional Swift
PPTX
Ppt on java basics
Keywords of java
JavaProgrammingManual
The Ring programming language version 1.8 book - Part 25 of 202
The what over the how (another way on android development with kotlin)
Operator overload rr
The Ring programming language version 1.5.1 book - Part 30 of 180
The Ring programming language version 1.9 book - Part 27 of 210
Python lecture 05
TeraSort
Image Recognition with Neural Network
Swift Tutorial 2
123
Clojure functions examples
The Ring programming language version 1.5.2 book - Part 31 of 181
The Ring programming language version 1.2 book - Part 21 of 84
The Ring programming language version 1.7 book - Part 35 of 196
Quick python reference
R programming
7 Habits For a More Functional Swift
Ppt on java basics
Ad

Similar to Functional programming with clojure (20)

PPT
PDF
Get into Functional Programming with Clojure
PDF
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PDF
Clojure intro
PDF
Fun with Functional Programming in Clojure
KEY
Scala: functional programming for the imperative mind
PDF
Thinking Functionally - John Stevenson - Codemotion Rome 2017
PDF
Thinking Functionally with Clojure
PDF
Functional web with clojure
PPTX
Functional Programming.pptx
PDF
Functional Programming with Clojure
PDF
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
KEY
The Joy Of Functional Programming
PDF
Functional programming
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
PDF
Functional Go
PDF
Thinking Functionally
PDF
Functional Programming #FTW
PPTX
Principles of functional progrmming in scala
Get into Functional Programming with Clojure
Fun with Functional Programming in Clojure - John Stevenson - Codemotion Amst...
Столпы функционального программирования для адептов ООП, Николай Мозговой
Clojure intro
Fun with Functional Programming in Clojure
Scala: functional programming for the imperative mind
Thinking Functionally - John Stevenson - Codemotion Rome 2017
Thinking Functionally with Clojure
Functional web with clojure
Functional Programming.pptx
Functional Programming with Clojure
Stefan Kanev: Clojure, ClojureScript and Why They're Awesome at I T.A.K.E. Un...
The Joy Of Functional Programming
Functional programming
LISP: How I Learned To Stop Worrying And Love Parantheses
Functional Go
Thinking Functionally
Functional Programming #FTW
Principles of functional progrmming in scala
Ad

Recently uploaded (20)

PDF
Introduction to Ragic - #1 No Code Tool For Digitalizing Your Business Proces...
PDF
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
PPTX
Airline CRS | Airline CRS Systems | CRS System
PPTX
Full-Stack Developer Courses That Actually Land You Jobs
PDF
Internet Download Manager IDM Crack powerful download accelerator New Version...
PDF
Microsoft Office 365 Crack Download Free
PPTX
Cybersecurity: Protecting the Digital World
PPTX
Matchmaking for JVMs: How to Pick the Perfect GC Partner
PDF
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
DOC
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
PPTX
HackYourBrain__UtrechtJUG__11092025.pptx
DOCX
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
PPTX
Computer Software - Technology and Livelihood Education
PPTX
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
PDF
Practical Indispensable Project Management Tips for Delivering Successful Exp...
PDF
Guide to Food Delivery App Development.pdf
PDF
CCleaner 6.39.11548 Crack 2025 License Key
PDF
Workplace Software and Skills - OpenStax
PPTX
Python is a high-level, interpreted programming language
Introduction to Ragic - #1 No Code Tool For Digitalizing Your Business Proces...
novaPDF Pro 11.9.482 Crack + License Key [Latest 2025]
Airline CRS | Airline CRS Systems | CRS System
Full-Stack Developer Courses That Actually Land You Jobs
Internet Download Manager IDM Crack powerful download accelerator New Version...
Microsoft Office 365 Crack Download Free
Cybersecurity: Protecting the Digital World
Matchmaking for JVMs: How to Pick the Perfect GC Partner
AI/ML Infra Meetup | Beyond S3's Basics: Architecting for AI-Native Data Access
How to Use SharePoint as an ISO-Compliant Document Management System
UTEP毕业证学历认证,宾夕法尼亚克拉里恩大学毕业证未毕业
HackYourBrain__UtrechtJUG__11092025.pptx
Modern SharePoint Intranet Templates That Boost Employee Engagement in 2025.docx
Computer Software - Technology and Livelihood Education
Cybersecurity-and-Fraud-Protecting-Your-Digital-Life.pptx
Practical Indispensable Project Management Tips for Delivering Successful Exp...
Guide to Food Delivery App Development.pdf
CCleaner 6.39.11548 Crack 2025 License Key
Workplace Software and Skills - OpenStax
Python is a high-level, interpreted programming language

Functional programming with clojure

  • 3. A comparison... Object-oriented Functional The HOW How to perform tasks and how to track changes in state The WHAT What information is desired and what transformations are required State changes are important Immutable state and data structures Loops, conditionals, and method calls Order of execution is important. Function calls and managing loops through recursion. Instances of structures or classes Functions as first-class objects and data collections.
  • 5. What is Clojure? ● Dialect of the Lisp programming language (+ 1 2) ● Hosted language running on the JVM (java virtual machine) ● Interoperable with the host language ● Written like a data structure
  • 7. Basic Clojure Syntax Define a value or data e.g. (def name “value”) Define a function e.g. (defn functionName [argument] (behavior)) Binding a value e.g. (let [value]) Collections: (list 1 2 3) => ‘(1 2 3) (vec ‘(1 2 3) => [1 2 3] //similar to an array, zero indexed (set ‘(1 2 1) => #{1 2} // set of unique values (map + [1 2 3] [4 5 6]) => (5 7 9)
  • 8. First-class functions ● Treats functions as values so you can assign it to a variable, pass it to other functions, store it in data structures e.g arrays, lists ● Can be used in higher-order functions - takes functions as arguments or returns a function as a result ● Constructed at runtime Why is this useful? ● When your inputs are yet to be provided ● reduce unnecessary code ● Increase reusability - keep it DRY
  • 9. First-class functions and higher order functions (first class function) => (def double-it (number) (fn [number] (* 2 number)) (higher-order function) => (map double-it [1 2 3]) => (2 4 6) (def double-it (partial * 2)) same as (fn [number] (* 2 number)) or #(* 2 %)
  • 10. Function Composition Takes a set of functions and returns a function that is the composition of those functions. (filter (comp not zero?) [0 1 0 2 0 3 0 4]) => (1 2 3 4)
  • 12. Pure function? (def age 20) (defn whats-my-age [name] (str name “: ” age)) => (whats-my-age “Prashant”) => “Prashant: 20”
  • 13. Impure Pure (def age 20) (defn my-age [name] (str name “:” age)) => (my-age “Prashant”) => “Prashant:20” Create another age... (defn my-age [name] (let [age 20] (str name “:” age))) => (my-age “Prashant”) => “Prashant:20”
  • 14. Libraries (:import java.util.Date) (defn race-complete [racer-name] (str race-name “ finished on "(java.util.Date.)) => (race-complete “Prashant") => "Prashant finished on Sun Oct 01 10:03:59 BST 2017" => (race-complete “Prashant") => "Prashant finished on Sun Oct 01 10:04:48 BST 2017"
  • 15. Almost a lottery scam! (defn scratchie-lottery [] (if (= (rand) 3) "Congrats! You’re a millionaire!" "Hahaha your money is mine!")) => (scratchie-lottery) => "Hahaha your money is mine!"
  • 16. Pure doesn’t always mean Idempotent A pure function - calling f(x) will give the same result no matter how many times you call it. E.g. (defn add-1 [n] (+ 1 n) An idempotent function f(f(x)) is the same as f(x). E.g. (clojure.string/upper-case (clojure.string/upper-case “hello”)) => “HELLO” (float (float 2))
  • 17. Why are pure functions useful? ● when given the same input, will always return the same output and does not have any observable side effect - you have CERTAINTY! ● makes testing much easier - we have less set up as we simply give it the input and assert on the output e.g. no need for a payment gateway ● they can be run anywhere, when do you find you are copying the code over?
  • 18. A value is something that doesn’t change. 20 is 20. 1 January 2017 is just that. $5 doesn’t become $10. My favorite fruits are bananas and cherries (hmmmm….but not together).
  • 19. What if my favorite fruit is now figs? Well it will be a different set. An identity with a state whose value is a value at a point in time.
  • 21. Immutability - Persistent data structures (list 1 2 3) => ‘(1 2 3) (vec ‘(1 2 3) => [1 2 3] //similar to an array, zero indexed (set ‘(1 2 1) => #{1 2} // set of unique values (map + [1 2 3] [4 5 6]) => (5 7 9)
  • 23. So what if we do need to change state? ● Memory state can be shared readily between threads which makes concurrency easier ● Software transactional memory (STM) supports sharing of changing state between threads in a synchronous and coordinated manner. ● Vars are for when you need to store something on a per-thread basis. If you have a multi-threaded program and each thread needs its own private state, put that state in a var. Coordinated Uncoordinated synchronous Ref (STM) atoms asynchronous - agents
  • 24. Recursion vs tail recursion Traditional recursive (defn factorial [n] (if (zero? n) 1 (* n (factorial (dec n))))) You don’t get the result of your calculation until you have returned from every recursive call. Generates a new stackframe for each call. Tail recursion (defn factorial [n] (loop [count n result 1] (if (pos? count) (recur (dec count) (* result count)) result ))) Processing occurs before recursive call. Reuse existing stackframes.
  • 25. Compare on the call stack Traditional recursive Tail recursion (factorial 5) : 5 * _ (factorial 4) : 4 * _ (factorial 3) : 3 * _ (factorial 2) : 2 * _ (factorial 1) : 1 * _ (factorial 0) : 1 (factorial 5) 1 1 2 6 24 (factorial 5) = 120 (factorial 5) = (loop 5 0)(loop 4 5)(loop 3 20)(loop 2 60)(loop 1 120) 120 count n
  • 26. Lazy evaluation ● Only return a value when necessary “call-by-need” ● Things are calculated not when they are defined, but when they are actually needed. ● Maintain precision ● Optimise evaluation Examples: ratios, lazy sequence such as range and repeat
  • 27. TDD using midje ● Test driven development - Red/Green/Refactor ● Clean, tested, lean ● Midje - testing framework for clojure
  • 28. Clojure resources - Lots of resources - Clojure documentation - https://www.braveclojure.com/ - https://clojure.org/community/books - Clojure koans - tryclj.com/ online REPL - https://clojure.org/api/cheatsheet