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

What's hot (20)

PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PDF
Kotlin Overview
Silicon Straits
 
PDF
Threequals - Case Equality in Ruby
Louis Scoras
 
PPTX
Kotlin presentation
MobileAcademy
 
PDF
Kotlin: Why Do You Care?
intelliyole
 
PDF
Practical Functional Programming Presentation by Bogdan Hodorog
3Pillar Global
 
PDF
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
PDF
Pipeline oriented programming
Scott Wlaschin
 
PPTX
Future of Kotlin - How agile can language development be?
Andrey Breslav
 
PPTX
Kotlin on android
Kurt Renzo Acosta
 
PDF
A quick and fast intro to Kotlin
XPeppers
 
PDF
Monte Carlo C++
Dmitri Nesteruk
 
PDF
Booting into functional programming
Dhaval Dalal
 
PDF
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
PDF
Coding for Android on steroids with Kotlin
Kai Koenig
 
PDF
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan s.r.o.
 
PDF
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
PDF
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
PDF
Kotlin: Challenges in JVM language design
Andrey Breslav
 
PDF
Functional go
Geison Goes
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Kotlin Overview
Silicon Straits
 
Threequals - Case Equality in Ruby
Louis Scoras
 
Kotlin presentation
MobileAcademy
 
Kotlin: Why Do You Care?
intelliyole
 
Practical Functional Programming Presentation by Bogdan Hodorog
3Pillar Global
 
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
 
Pipeline oriented programming
Scott Wlaschin
 
Future of Kotlin - How agile can language development be?
Andrey Breslav
 
Kotlin on android
Kurt Renzo Acosta
 
A quick and fast intro to Kotlin
XPeppers
 
Monte Carlo C++
Dmitri Nesteruk
 
Booting into functional programming
Dhaval Dalal
 
Game of Life - Polyglot FP - Haskell, Scala, Unison - Part 2 - with minor cor...
Philip Schwarz
 
Coding for Android on steroids with Kotlin
Kai Koenig
 
eMan Dev Meetup: Kotlin For Android (part 03/03) 18.5.2017
eMan s.r.o.
 
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
Kotlin: Challenges in JVM language design
Andrey Breslav
 
Functional go
Geison Goes
 

Similar to Good functional programming is good programming (20)

PDF
OOP and FP
Mario Fusco
 
PPTX
The joy of functional programming
Steve Zhang
 
PPT
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
PPT
Functional Programming Past Present Future
IndicThreads
 
PDF
A taste of Functional Programming
Jordan Open Source Association
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PPTX
Intro to Functional Programming
Jordan Parmer
 
PDF
Functional Programming Principles & Patterns
zupzup.org
 
PPS
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
PPTX
Functional programming
Prashant Kalkar
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PDF
Go Beyond Higher Order Functions: A Journey into Functional Programming
Lex Sheehan
 
PDF
Functional Programming #FTW
Adriano Bonat
 
KEY
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
PPTX
Can programming be liberated from the von neumann style?
Oriol López Massaguer
 
PPTX
Functional programming and ruby in functional style
Niranjan Sarade
 
PDF
Functional Programming with Groovy
Arturo Herrero
 
PDF
Demystify Functional Programming in Swift
Ennio Masi
 
PDF
A (very brief) into to Functional Programming
Brooklyn Zelenka
 
PDF
Ti1220 Lecture 7: Polymorphism
Eelco Visser
 
OOP and FP
Mario Fusco
 
The joy of functional programming
Steve Zhang
 
Functional Programming - Past, Present and Future
Pushkar Kulkarni
 
Functional Programming Past Present Future
IndicThreads
 
A taste of Functional Programming
Jordan Open Source Association
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Intro to Functional Programming
Jordan Parmer
 
Functional Programming Principles & Patterns
zupzup.org
 
Presentation of GetTogether on Functional Programming
Filip De Sutter
 
Functional programming
Prashant Kalkar
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Lex Sheehan
 
Functional Programming #FTW
Adriano Bonat
 
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
 
Can programming be liberated from the von neumann style?
Oriol López Massaguer
 
Functional programming and ruby in functional style
Niranjan Sarade
 
Functional Programming with Groovy
Arturo Herrero
 
Demystify Functional Programming in Swift
Ennio Masi
 
A (very brief) into to Functional Programming
Brooklyn Zelenka
 
Ti1220 Lecture 7: Polymorphism
Eelco Visser
 
Ad

More from kenbot (9)

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

Recently uploaded (20)

PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 

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