SlideShare a Scribd company logo
(= (good (functional programming))
(good programming))
@KenScambler
What is functional
programming?
λx → x+1
What is a functional
programming
language?
(lambda (x)
(+ x 1))
do |x|
x + 1
end
new Function<Integer,Integer>() {
@Override
public Integer apply(Integer x) {
return x + 1;
}
};
You can do FP in any
language!
John McCarthy
1927 - 2011
Lisp, 1958
(DEFUN FAC (N)
(IF (= N 0)
1
(* N (FAC (- N 1)))))
• Value-oriented
• Garbage collection
• Lambda functions
Guy Steele
1954-
Scheme, 1970
(define (fac n)
(if (= n 0)
1
(* n (fac (- n 1))))))
• Lexical scope
• Tail call optimization
• Continuations
Gerald Sussman
1947-
Robin Milner, et al
1934 - 2010
ML, 1973
fun fac (0 : int) : int = 1 |
fac (n : int) : int =
n * fac (n - 1)
• Statically typed
• HM type inference
• ADTs, Pattern matching
Haskell Committee
Haskell, 1990
fac :: (Integral a) => a -> a
fac n = if n < 2
then 1
else n * fac (n-1)
• Purity
• Lazy evaluation
• Monads
Why do I care?
The things that make FP good
are the same things that make
good software good
Simple Modular Local reasoning
Extensible Testable
Think better thoughts
Your design ideas are hi-res
Think better thoughts
Your ideas expressed in a
language are low-res
If you only think “in a
language”, then your ideas
are already low res
Functions & composition
Immutability Purity & types
The big ideas
λ
Laziness
Functionsλ
Good functional programming is good programming
Good functional programming is good programming
Composition
A  B
A  B
B  C
A  B
B  C
C  D
A  C
C  D
A  D
Equational reasoning
5 + (3 * 4)
Equational reasoning
5 + 12
Equational reasoning
17
Equational reasoning
process(
expensiveThing(),
expensiveThing(),
expensiveThing())
Equational reasoning
val x = expensiveThing()
process(x,x,x)
Referential Transparency
“An expression is said to be
referentially transparent if it can be
replaced with its corresponding value
without changing the program's
behavior.”
- Wikipedia
It’s just good sense!
Being able to reason about software is
just good programming!
Immutability
case class Point(
x: Int,
y: Int)
• Immutable
• Instances are values
• Simple!
case class Point(
var x: Int,
var y: Int)
• Mutable
• Instances are identities that track a
series of Point states over time, where
you only get to see the latest one
• Not simple
"Classes should be immutable unless there's a
very good reason to make them mutable....If a
class cannot be made immutable, limit its
mutability as much as possible."
- Joshua Bloch
Effective Java
“But the real world is mutable!”
- Programmers
Entities vs value types
“Some objects are not defined primarily by
their attributes. They represent a thread of
identity that runs through time and often
across distinct representations.”
- Eric Evans
Domain Driven Design
Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “bald”
=> “actor”
Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “bald”
=> “actor”
in 2017…
Entities vs value types
samuel_l_jackson.hairstyle
samuel_l_jackson.occupation
=> “afro”
=> “student”
1960s:
“Samuel L
Jackson”
H: “bald”
O: “actor”
H: “afro”
O: “student”
Identity
1960 1970 1980 1990 2000 2010
Persistent collections
Immutability is just good
programming!
Purity & types
Null is a 3rd world problem
Cake cake = null;
cake.cut();
Cake cake = null;
cake.cut();
Sum types
Maybe<A>
Just(A a)
NothingOR
Sum types (Haskell)
data Maybe a =
Just a | Nothing
Sum types (Scala)
sealed trait Maybe[+A]
case class Just[A](a: A)
extends Maybe[A]
case object None
extends Maybe[Nothing]
Sum types (Java)
public class Maybe<A> {
private Maybe() {}
public abstract <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent);
public static class Just<A> extends Maybe<A> {
private final A a;
public Just(A a) {
this.a = a;
}
public <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent) {
ifPresent.apply(a);
}
}
public static class Nothing<A> extends Maybe<A> {
public <B> B fold(Function<A,B> ifPresent, Supplier<B> ifAbsent) {
ifAbsent.get();
}
}
}
Optional<Cake> cake =
Optional.empty());
cake.ifPresent(Cake::cut);
public Session login(
String userName,
String pwd) throws
AuthenticationException
public Session login(
String userName,
String pwd) throws
AuthenticationException
public Either<AuthError, Session>
login(
String userName,
String pwd)
Make illegal states
unrepresentable
Eradicable 3rd world diseases
Nulls
Exceptions
Fall-through cases
What can we know from a type?
process :: a
-> (a -> Bool)
-> WriterT Log (Reader Config) a
• Takes a predicate that operates only on the given item
• We do nothing with the “a” value other than filter it and return it
• No side effects are performed
• We might write an accumulating “Log” value
• The code declares what will take place, but does not execute it when called
• A “Config” will be required before the code can be eventually executed
Laziness
Separation of concerns!
Planning
Composition
Declaration
vs
Execution
Evaluation
Interpretation
(define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
from SICP
(define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Test each number to see if
it is a prime
(define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Accumulate total sum
(define (sum-primes a b)
(define (iter count accum)
(cond ((> count b) accum)
((prime? count)
(iter (+ count 1)
(+ count accum)))
(else (iter (+ count 1) accum))))
(iter a 0))
Sum primes, iteratively
Efficiency 👍
• The number range is only
traversed once
Modularity 👎
• Prime test and summation all
smooshed together in loop
(define (sum-primes a b)
(accumulate +
0
(filter prime?
(enumerate-interval a b))))
Sum primes, with list ops
Efficiency 👎
• The number list is traversed
several times
Modularity 👍
• Prime test impl & summation
impl just plug in together
(define (sum-primes a b)
(accumulate +
0
(stream-filter prime?
(enumerate-stream-interval
a b))))
Sum primes, with lazy stream ops
Modularity 👍
• Prime test impl & summation
impl just plug in together
Efficiency 👍
• The number range is only
traversed once
“Lazy evaluation is perhaps the most
powerful tool
for modularization in the functional
programmer’s repertoire..”
- John Hughes
Why Functional
Programming Matters
Living the dream
Imagine a world where…
You don’t have to chase down
runtime stack traces in the logs,
because they can’t happen
Living the dream
Imagine a world where…
Testing software is really easy
Living the dream
Imagine a world where…
You can write software by clicking
typed functions together like Lego
Living the dream
Imagine a world where…
You can reason about one thing at
a time without cramming the
whole world in
User groups
Melbourne Functional User Group
https://www.meetup.com/en-AU/Melbourne-Functional-User-
Group-MFUG/
Melbourne Scala User Group
https://www.meetup.com/en-AU/Melbourne-Scala-User-Group/
Melbourne Haskell Users Group
https://www.meetup.com/en-AU/Melbourne-Haskell-Users-Group/
clj-melb
https://www.meetup.com/en-AU/clj-melb/

More Related Content

PPTX
2 Years of Real World FP at REA
PPTX
The disaster of mutable state
PPTX
Applied category theory: the emerging science of compositionality
PDF
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
PDF
Introduction to functional programming (In Arabic)
PDF
Swift and Kotlin Presentation
PDF
Ankara Jug - Practical Functional Programming with Scala
PPT
The Kotlin Programming Language
2 Years of Real World FP at REA
The disaster of mutable state
Applied category theory: the emerging science of compositionality
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
Introduction to functional programming (In Arabic)
Swift and Kotlin Presentation
Ankara Jug - Practical Functional Programming with Scala
The Kotlin Programming Language

What's hot (20)

PPTX
Introduction to Kotlin Language and its application to Android platform
PDF
Kotlin Overview
PDF
Threequals - Case Equality in Ruby
PPTX
Kotlin presentation
PDF
Kotlin: Why Do You Care?
PDF
Practical Functional Programming Presentation by Bogdan Hodorog
PDF
Thinking in Functions: Functional Programming in Python
PDF
Pipeline oriented programming
PPTX
Future of Kotlin - How agile can language development be?
PPTX
Kotlin on android
PDF
A quick and fast intro to Kotlin
PDF
Monte Carlo C++
PDF
Booting into functional programming
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
PDF
Coding for Android on steroids with Kotlin
PDF
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
PDF
(How) can we benefit from adopting scala?
PDF
Functional Programming Patterns (BuildStuff '14)
PDF
Kotlin: Challenges in JVM language design
PDF
Functional go
Introduction to Kotlin Language and its application to Android platform
Kotlin Overview
Threequals - Case Equality in Ruby
Kotlin presentation
Kotlin: Why Do You Care?
Practical Functional Programming Presentation by Bogdan Hodorog
Thinking in Functions: Functional Programming in Python
Pipeline oriented programming
Future of Kotlin - How agile can language development be?
Kotlin on android
A quick and fast intro to Kotlin
Monte Carlo C++
Booting into functional programming
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Coding for Android on steroids with Kotlin
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
(How) can we benefit from adopting scala?
Functional Programming Patterns (BuildStuff '14)
Kotlin: Challenges in JVM language design
Functional go
Ad

Similar to Good functional programming is good programming (20)

PDF
Monadologie
PPTX
Functional programming
PPTX
Yin Yangs of Software Development
PDF
Functional programming ii
PDF
Monads and Monoids by Oleksiy Dyagilev
PPT
An introduction to scala
PDF
TI1220 Lecture 6: First-class Functions
PDF
Power of functions in a typed world
PDF
Kotlin Austin Droids April 14 2016
PPTX
Fun with Lambdas: C++14 Style (part 2)
PDF
Why Haskell Matters
PDF
Hw09 Hadoop + Clojure
PDF
Scala: Functioneel programmeren in een object georiënteerde wereld
PDF
Lecture 5: Functional Programming
PDF
Scalapeno18 - Thinking Less with Scala
PDF
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
PDF
Coding in Style
PDF
A taste of Functional Programming
PPTX
PDF
Hadoop + Clojure
Monadologie
Functional programming
Yin Yangs of Software Development
Functional programming ii
Monads and Monoids by Oleksiy Dyagilev
An introduction to scala
TI1220 Lecture 6: First-class Functions
Power of functions in a typed world
Kotlin Austin Droids April 14 2016
Fun with Lambdas: C++14 Style (part 2)
Why Haskell Matters
Hw09 Hadoop + Clojure
Scala: Functioneel programmeren in een object georiënteerde wereld
Lecture 5: Functional Programming
Scalapeno18 - Thinking Less with Scala
Functional Programming You Already Know - Kevlin Henney - Codemotion Rome 2015
Coding in Style
A taste of Functional Programming
Hadoop + Clojure
Ad

More from kenbot (9)

PPTX
Grow your own tech leads
PPTX
Responsible DI: Ditch the Frameworks
PPTX
FP adoption at REA
PPTX
Lenses for the masses - introducing Goggles
PPTX
Data made out of functions
PPTX
Imagine a world without mocks
PPTX
Your data structures are made of maths!
PPTX
Category theory for beginners
PPTX
Running Free with the Monads
Grow your own tech leads
Responsible DI: Ditch the Frameworks
FP adoption at REA
Lenses for the masses - introducing Goggles
Data made out of functions
Imagine a world without mocks
Your data structures are made of maths!
Category theory for beginners
Running Free with the Monads

Recently uploaded (20)

PDF
Understanding Forklifts - TECH EHS Solution
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
top salesforce developer skills in 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
history of c programming in notes for students .pptx
Understanding Forklifts - TECH EHS Solution
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
VVF-Customer-Presentation2025-Ver1.9.pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How Creative Agencies Leverage Project Management Software.pdf
Odoo POS Development Services by CandidRoot Solutions
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms II-SECS-1021-03
top salesforce developer skills in 2025.pdf
Digital Strategies for Manufacturing Companies
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ISO 45001 Occupational Health and Safety Management System
Upgrade and Innovation Strategies for SAP ERP Customers
Navsoft: AI-Powered Business Solutions & Custom Software Development
history of c programming in notes for students .pptx

Good functional programming is good programming

Editor's Notes

  • #3: Powerful source of good software ideas Lambda functions Immutability Tuples Data classes Pattern matching Garbage Collection