SlideShare a Scribd company logo
1 of 114
Introduction to
Functional
Programming
hello!
Jordan Parmer
@ProfParmer
What is Functional
Programming?
A Very Short Story
“
“We are builders first;
technologists second
“The better builders we are, the
more value we provide.
1.
What is Functional
Programming?
What Does It Mean to be Imperative?
What Are The Most
Common Problems In
Imperative OOP Code?
Null Pointer Exceptions
Concurrency Errors With Shared State
Broad Context of Reasoning
Deep Inheritance Chains
Mutable State
What is the emphasis
with imperative OOP
code?
Inheritance over composition
Abstraction by Inheritance
Internal class state and encapsulation
Imperative state mutations
Can We Do Better?
Return to Roots
“A functional programming language is
a language that emphasises
programming with pure functions and
immutable data
Functional Programming
Declarative
Describe what, not how
Expressions over statements
Idempotent
Immutability
Functions are without side-effects
Results deterministic
Referentially Transparent
Any expression can be replaced with
the results of its value
Algebraic
Types, equality, expressions
Category theory
Functionally Composable
Separate behavior from data
Small functions composed together
to make small building blocks
Parallelizable
Idempotent code by nature enables
local and distributed parallelization
Myths
✘Requires a math degree to understand
✘Targets only math/science problems
✘Only relevant in academic circles
✘Not for “Line of Business” or “Form Over Data” apps
LIES!
Shocking Adjustments
✘No Null
✘Recursion over looping
✘Avoidance of if-statements
✘Map/filter/fold over iterating
✘Monadic I/O
✘Lots of foreign mathematical nomenclature
✘No debuggers
“OOP makes code understandable by
encapsulating moving parts.
FP makes code understandable by
minimizing moving parts.
✘Michael Feathers
We Want
These
You Don’t Need a
Functional Language To
Be Functional
But It Does Help
Some [Subjective] Examples
Pure-ish
✘ Haskell
✘ Erlang
✘ Elm
✘ Hope
✘ Joy
✘ Mercury
✘ Scheme
Hybrids
✘ Scala
✘ F#
✘ Kotlin
✘ Swift
✘ ML
✘ LISP
✘ Rust
✘ Groovy
✘ R
✘ Dart
✘ Ruby
Progressively Incorporating
✘ Java 8
✘ C#
✘ Python
✘ C++ 11
...and many more
Popular Languages
Beware Bias Ahead
JVM Languages
Scala
✘ Statically strong typed
✘ FP with OOP good parts
✘ Higher-order functions
✘ Insanely advanced type system
✘ Currying, lazy-evaluation, pattern matching, etc.
✘ Large commercial adoption
Twitter, Spotify, LinkedIn, Apache Spark, Kafka, etc.
✘ Backed by Lightbend + Scala Centre
Clojure
✘ Dynamically typed
✘ First-order functions
✘ S-expressions
✘ LISP-dialect
“Modern LISP that is symbiotic with Java”
.NET Languages
F#
✘ Statically typed
✘ Multi-paradigm: FP, imperative, OOP
✘ .NET Framework
✘ Visual Studio Tooling
✘ Backed by Microsoft
Apple Languages
Swift
✘ Scala...brought to you by Apple
I kid! I kid!
✘ FP alternative to Objective C
✘ Borrowed some constructs from C & Objective C
✘ Optional types
✘ Categories (i.e. type-classes)
Erlang Languages
Erlang
✘ Ideal for distributed systems
✘ Fault-tolerant, real-time
✘ Highly-available systems
✘ BEAM virtual machine
Elixir
✘ Higher abstractions on top of Erlang
Reduces boilerplate
✘ Runs on BEAM
✘ New language but closely related to Erlang
Purist Languages
Haskell
✘ Purely functional
✘ Named after Haskell Curry
✘ Strong type system
✘ Introduced type-classes
✘ Strict immutability
Elm
✘ FP for the web
✘ Graphical layout without destructive updates
✘ Interops with (not compiles to) JavaScript/CSS/HTML
...And So Many More
But What Does That All
Mean?
Let’s Talk About Some FP
Concepts
The Suspects
Pure Functions
Expressions
Type Systems
Function Chaining
Map, Filter, Fold
Functors, Monoids, Monads
Side-Effects Are The
Source of Many Woes
Consider
Algebra
f(x) = 3x2-7x+5Given the above
Find f(-4)
f(x) = 3x2-7x+5Given the above
Find f(-4)
81
Function Has Side-Effects If It...
✘Modifies a variable
✘Modifies a data structure in place
e.g. append to linked list
✘Throws an exception
✘Prints to the console
✘Writes to a file
✘Updates a database
✘Draws to the screen
Function Has Side-Effects If It...
✘Modifies a variable
✘Modifies a data structure in place
e.g. append to linked list
✘Throws an exception
✘Prints to the console
✘Writes to a file
✘Updates a database
✘Draws to the screen Have you ever passed a
reference of a list to
something else?
Single Input
Single Output
No side effects
Referentially transparent Func
A B
Function Is Pure If It...
Why is this Good?
Functional Imperative OOP
Pure functions are
easy to test
Local reasoning
In => Out
VS
Objects are hard to
test
Encapsulated state
Context
Global reasoning
Pure functions are
easy to re-use
Low risk
Little-to-no
dependencies
VS
We are terrible at
making truly reusable
objects
Challenges of tight
coupling
Hard to abstract at
correct level
Functional Imperative OOP
Pure functions are
easy to parallelize
State in isolation
Just lego pieces
VS
Sharing is cool in
life...not in threads
How many
concurrency patterns
do we need to deal
with shared state?
Functional Imperative OOP
Pure functions reduce
need for most OOP
design patterns
VS
‘Cause who doesn’t
want to know about
your
MVVMVMVMMVCCC
singleton factory
observer adapter
thingy?
Functional Imperative OOP
Function Composition
Single Input Single Output
Func
A B
Functions Of This Sort Can Be Things
Func
A B
Functions Of This Sort Can Be Things
Func
A B
Two Functions
Func
Func
Compose Them Together
Func
Func
Compose Them Together
Func
Func
New Function!
Func
Function Composition Is
The New Old Injection
// Goal: Allow brewing coffee with different techniques and beans
// Types
// Beans => Grind
// Grind => Coffee Compose to get: Beans => Coffee
trait Beans
trait Grind
trait Coffee
case class ColumbianBeans() extends Beans
def brew(grind: Beans => Grind)(brew: Grind => Coffee): Beans => Coffee = {
brew compose grind
}
def burrGrind(beans: Beans): Grind = ???
def blendGrind(beans: Beans): Grind = ???
def pourOver(grind: Grind): Coffee = ???
def drip(grind: Grind): Coffee = ???
def frenchPress(grind: Grind): Coffee = ???
val pourOverWithBurrGrind = brew(burrGrind)(pourOver)
val myFavoriteBeans = ColumbianBeans()
val myFavoriteCoffee = pourOverWithBurrGrind(myFavoriteBeans)
// TIME TO WAKE UP!!!
// Goal: Allow brewing coffee with different techniques and beans
// Types
// Beans => Grind
// Grind => Coffee Compose to get: Beans => Coffee
trait Beans
trait Grind
trait Coffee
case class ColumbianBeans() extends Beans
def brew(grind: Beans => Grind)(brew: Grind => Coffee): Beans => Coffee = {
brew compose grind
}
def burrGrind(beans: Beans): Grind = ???
def blendGrind(beans: Beans): Grind = ???
def pourOver(grind: Grind): Coffee = ???
def drip(grind: Grind): Coffee = ???
def frenchPress(grind: Grind): Coffee = ???
val pourOverWithBurrGrind = brew(burrGrind)(pourOver)
val myFavoriteBeans = ColumbianBeans()
val myFavoriteCoffee = pourOverWithBurrGrind(myFavoriteBeans)
// TIME TO WAKE UP!!!
brew is a higher
order function
Why Useful?
// Imagine how many combinations we can use this code
// Imagine how easy testing is
object Person {
def drink(coffee: Coffee): AwakePerson = ???
}
def wakeSomeone(makeIt: () => Coffee): AwakePerson = {
Person.drink(makeIt())
}
Anything that
makes coffee
Expressions
// Which right triangle that has integers for all sides and all sides equal to
// or smaller than 10 has a perimeter of 24?
def rightTriangle: Seq[Int] =
for {
c <- 1 to 10
b <- 1 to c
a <- 1 to b
if ((Math.pow(a,2) + Math.pow(b,2) == Math.pow(c,2)) && (a + b + c == 24))
} yield (a, b, c)
rightTriangle
Answer: [6, 8, 10]
It’s Not About the How
but the What
Type Constraints
f(x) = 3/x
Int => Double
f(x) = 3/x
1 => 3/1 => 3.0
2 => 3/2 => 1.5
0 => 3/0 => ???
f(x) = 3/x
1 => 3/1 => 3.0
2 => 3/2 => 1.5
0 => 3/0 => ???
AH,
PICKLES!
f(x) = 3/x
1 => 3/1 => 3.0
2 => 3/2 => 1.5
0 => 3/0 => ???
THROW AN
EXCEPTION?
f(x) = 3/x
Int => Double
0 => 3/0 => ???
THROW AN
EXCEPTION?
THEN THIS
IS A LIE!
f(x) = 3/x
Int => Double
0 => 3/0 => ???
THROW AN
EXCEPTION?
THEN THIS
IS A LIE!
Are You A
Liar?
But Functions Are Types!
Types Don’t Have to Be
Open to All
Circumstances
f(x) = 3/x
NonZeroInt => Double
Just Became Constrained And
Self-Documented
f(x) = 3/x
Zero Not
Allowed BY
NonZeroInt
-1 => 3/-1 => -3
1 => 3/1 => 3
2 => 3/2 => 1.5
Static Types Just Became
Enforced Domain
Documentation
We Sometimes Refer to
Our Programs as an
“Algebra”
If It Compiles, It
Probably Works
Function Chaining
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z = DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z = DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Yeah, I know
this is bad.
But you know
you’ve seen
this before.
Hang with me.
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z =
DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Pyramid
of Doom
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z =
DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Pyramid
of Doom
The Only
Lines Doing
Work
That’s only
20%!
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z =
DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Pyramid
of Doom
Look at all
those nulls!
Half this code
is null
checking.
Nulls are a
serious code
smell!
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z =
DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Pyramid
of Doom
Look at all
those nulls!
Half this code
is null
checking.
Nulls are a
serious code
smell!
Null Pointer
Exceptions
Waiting to
Happen
def foo(): Thing = {
val x = DoAThing()
if (x != null) {
val y = DoAnotherThing(x)
if (y != null) {
val z =
DoYetAnotherThing(y)
if (z != null) {
return z
} else {
return null
}
} else {
return null
}
} else {
return null
}
}
Pyramid
of Doom
Throw in async
operations, we can
create the same
pyramid with nested
callbacks.
FP to the Rescue!
def foo(): Option[Int] = {
for {
x <- DoAThing()
y <- DoAnotherThing(x)
z <- DoYetAnotherThing(y)
} yield z
}
def foo(): Option[Int] = {
for {
x <- DoAThing()
y <- DoAnotherThing(x)
z <- DoYetAnotherThing(y)
} yield z
}
Every line is
significant
def foo(): Option[Int] = {
for {
x <- DoAThing()
y <- DoAnotherThing(x)
z <- DoYetAnotherThing(y)
} yield z
}
Every line is
significant
Code Examples
Application Becomes a
Collection of Pure
Functions
Just Fold the Collection
Parallelization
Fold All the Pieces!
Monads
The Key to a Lot of Awesome
Functors, Monoids, and
Bears
Functor
A => B
Monoid
Type A
Identity Function
Binary Combinator
Monad
Type with a Functor +
FlatMap
State
State
State stays in
box
State
The box is a
“monad”
State
Think of
monad as
collection of
at most one
item
State
You can do collection things to monads
Operation creates new monad of same type but
new state
.map(...)
.flatMap(...)
.fold(...)
.filter(...)
.collect(...)
.head
State
If Monad is “empty”, applied functions are
ignored
.map(...)
.flatMap(...)
.fold(...)
.filter(...)
.collect(...)
.head
def foo(): Option[Int] = Some(100)
val myFoo = foo()
val result =
myFoo
.map(i => i + 100)
.map(i => i.toString)
.map(i => i + " is a bigger number")
.getOrElse("didn't do anything")
// result = "200 is a bigger number"
def foo(): Option[Int] = None
val myFoo = foo()
val result =
myFoo
.map(i => i + 100)
.map(i => i.toString)
.map(i => i + " is a bigger number")
.getOrElse("didn't do anything")
// result = "didn’t do anything"
Executed but
not applied
Monads are so much
more but let’s keep it
simple for now…
...another talk to come
2.
Common Applications
Trends in Functional Programming
Data Processing Streaming Parallelization
Machine Learning
Heavy
Computation Attracting Talent
3.
Disadvantages?
4.
Final Thoughts
Natural way to migrate
To Functional
Programming?
thanks!
Any questions?
You can find me at
@ProfParmer

More Related Content

What's hot

Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming FundamentalsShahriar Hyder
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scalaAmuhinda Hungai
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Vadim Dubs
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Chris Richardson
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardKelsey Gilmore-Innis
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellBryan O'Sullivan
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: NotesRoberto Casadei
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBryan O'Sullivan
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardMario Fusco
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overridingRajab Ali
 

What's hot (20)

Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Introduction to programming in scala
Introduction to programming in scalaIntroduction to programming in scala
Introduction to programming in scala
 
Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.Javaz. Functional design in Java 8.
Javaz. Functional design in Java 8.
 
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...Map(), flatmap() and reduce() are your new best friends: simpler collections,...
Map(), flatmap() and reduce() are your new best friends: simpler collections,...
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
DEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World HaskellDEFUN 2008 - Real World Haskell
DEFUN 2008 - Real World Haskell
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
 
BayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore HaskellBayFP: Concurrent and Multicore Haskell
BayFP: Concurrent and Multicore Haskell
 
Monadic Java
Monadic JavaMonadic Java
Monadic Java
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
Functional Programming in Scala
Functional Programming in ScalaFunctional Programming in Scala
Functional Programming in Scala
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Java 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forwardJava 7, 8 & 9 - Moving the language forward
Java 7, 8 & 9 - Moving the language forward
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 

Viewers also liked

Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013ScalaNsk
 
Функциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьФункциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьCUSTIS
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersDiego Freniche Brito
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8LivePerson
 
Quantum computing - Introduction
Quantum computing - IntroductionQuantum computing - Introduction
Quantum computing - Introductionrushmila
 

Viewers also liked (6)

Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013Павел Павлов - Scala для профессионалов - Joker 2013
Павел Павлов - Scala для профессионалов - Joker 2013
 
Insight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FPInsight into java 1.8, OOP VS FP
Insight into java 1.8, OOP VS FP
 
Функциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальностьФункциональное программирование: мифы и реальность
Функциональное программирование: мифы и реальность
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Quantum computing - Introduction
Quantum computing - IntroductionQuantum computing - Introduction
Quantum computing - Introduction
 

Similar to Intro to Functional Programming

Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmerShawn Button
 
Douglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainDouglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainWeb Directions
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
Section 8 Programming Style and Your Brain: Douglas Crockford
Section 8 Programming Style and Your Brain: Douglas CrockfordSection 8 Programming Style and Your Brain: Douglas Crockford
Section 8 Programming Style and Your Brain: Douglas Crockfordjaxconf
 
Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Calvin Cheng
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Duckademy IT courses
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!Gautam Rege
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 
name name2 n
name name2 nname name2 n
name name2 ncallroom
 

Similar to Intro to Functional Programming (20)

Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
Fp for the oo programmer
Fp for the oo programmerFp for the oo programmer
Fp for the oo programmer
 
Douglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your BrainDouglas Crockford - Programming Style and Your Brain
Douglas Crockford - Programming Style and Your Brain
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Section 8 Programming Style and Your Brain: Douglas Crockford
Section 8 Programming Style and Your Brain: Douglas CrockfordSection 8 Programming Style and Your Brain: Douglas Crockford
Section 8 Programming Style and Your Brain: Douglas Crockford
 
OOP and FP
OOP and FPOOP and FP
OOP and FP
 
Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...Introduction to Java programming - Java tutorial for beginners to teach Java ...
Introduction to Java programming - Java tutorial for beginners to teach Java ...
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Laravel level 0 (introduction)
Laravel level 0 (introduction)Laravel level 0 (introduction)
Laravel level 0 (introduction)
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 

Recently uploaded

Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...Akihiro Suda
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Rob Geurden
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
20240415 [Container Plumbing Days] Usernetes Gen2 - Kubernetes in Rootless Do...
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...Simplifying Microservices & Apps - The art of effortless development - Meetup...
Simplifying Microservices & Apps - The art of effortless development - Meetup...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

Intro to Functional Programming

  • 4. A Very Short Story
  • 5.
  • 6. “We are builders first; technologists second
  • 7. “The better builders we are, the more value we provide.
  • 9. What Does It Mean to be Imperative?
  • 10. What Are The Most Common Problems In Imperative OOP Code?
  • 11. Null Pointer Exceptions Concurrency Errors With Shared State Broad Context of Reasoning Deep Inheritance Chains Mutable State
  • 12. What is the emphasis with imperative OOP code?
  • 13. Inheritance over composition Abstraction by Inheritance Internal class state and encapsulation Imperative state mutations
  • 14. Can We Do Better?
  • 16. “A functional programming language is a language that emphasises programming with pure functions and immutable data
  • 17. Functional Programming Declarative Describe what, not how Expressions over statements Idempotent Immutability Functions are without side-effects Results deterministic Referentially Transparent Any expression can be replaced with the results of its value Algebraic Types, equality, expressions Category theory Functionally Composable Separate behavior from data Small functions composed together to make small building blocks Parallelizable Idempotent code by nature enables local and distributed parallelization
  • 18. Myths ✘Requires a math degree to understand ✘Targets only math/science problems ✘Only relevant in academic circles ✘Not for “Line of Business” or “Form Over Data” apps
  • 19. LIES!
  • 20. Shocking Adjustments ✘No Null ✘Recursion over looping ✘Avoidance of if-statements ✘Map/filter/fold over iterating ✘Monadic I/O ✘Lots of foreign mathematical nomenclature ✘No debuggers
  • 21. “OOP makes code understandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. ✘Michael Feathers
  • 23. You Don’t Need a Functional Language To Be Functional
  • 24. But It Does Help
  • 25. Some [Subjective] Examples Pure-ish ✘ Haskell ✘ Erlang ✘ Elm ✘ Hope ✘ Joy ✘ Mercury ✘ Scheme Hybrids ✘ Scala ✘ F# ✘ Kotlin ✘ Swift ✘ ML ✘ LISP ✘ Rust ✘ Groovy ✘ R ✘ Dart ✘ Ruby Progressively Incorporating ✘ Java 8 ✘ C# ✘ Python ✘ C++ 11 ...and many more
  • 27. JVM Languages Scala ✘ Statically strong typed ✘ FP with OOP good parts ✘ Higher-order functions ✘ Insanely advanced type system ✘ Currying, lazy-evaluation, pattern matching, etc. ✘ Large commercial adoption Twitter, Spotify, LinkedIn, Apache Spark, Kafka, etc. ✘ Backed by Lightbend + Scala Centre Clojure ✘ Dynamically typed ✘ First-order functions ✘ S-expressions ✘ LISP-dialect “Modern LISP that is symbiotic with Java”
  • 28. .NET Languages F# ✘ Statically typed ✘ Multi-paradigm: FP, imperative, OOP ✘ .NET Framework ✘ Visual Studio Tooling ✘ Backed by Microsoft
  • 29. Apple Languages Swift ✘ Scala...brought to you by Apple I kid! I kid! ✘ FP alternative to Objective C ✘ Borrowed some constructs from C & Objective C ✘ Optional types ✘ Categories (i.e. type-classes)
  • 30. Erlang Languages Erlang ✘ Ideal for distributed systems ✘ Fault-tolerant, real-time ✘ Highly-available systems ✘ BEAM virtual machine Elixir ✘ Higher abstractions on top of Erlang Reduces boilerplate ✘ Runs on BEAM ✘ New language but closely related to Erlang
  • 31. Purist Languages Haskell ✘ Purely functional ✘ Named after Haskell Curry ✘ Strong type system ✘ Introduced type-classes ✘ Strict immutability Elm ✘ FP for the web ✘ Graphical layout without destructive updates ✘ Interops with (not compiles to) JavaScript/CSS/HTML
  • 33. But What Does That All Mean?
  • 34. Let’s Talk About Some FP Concepts
  • 35. The Suspects Pure Functions Expressions Type Systems Function Chaining Map, Filter, Fold Functors, Monoids, Monads
  • 38. f(x) = 3x2-7x+5Given the above Find f(-4)
  • 39. f(x) = 3x2-7x+5Given the above Find f(-4) 81
  • 40. Function Has Side-Effects If It... ✘Modifies a variable ✘Modifies a data structure in place e.g. append to linked list ✘Throws an exception ✘Prints to the console ✘Writes to a file ✘Updates a database ✘Draws to the screen
  • 41. Function Has Side-Effects If It... ✘Modifies a variable ✘Modifies a data structure in place e.g. append to linked list ✘Throws an exception ✘Prints to the console ✘Writes to a file ✘Updates a database ✘Draws to the screen Have you ever passed a reference of a list to something else?
  • 42. Single Input Single Output No side effects Referentially transparent Func A B Function Is Pure If It...
  • 43. Why is this Good?
  • 44. Functional Imperative OOP Pure functions are easy to test Local reasoning In => Out VS Objects are hard to test Encapsulated state Context Global reasoning
  • 45. Pure functions are easy to re-use Low risk Little-to-no dependencies VS We are terrible at making truly reusable objects Challenges of tight coupling Hard to abstract at correct level Functional Imperative OOP
  • 46. Pure functions are easy to parallelize State in isolation Just lego pieces VS Sharing is cool in life...not in threads How many concurrency patterns do we need to deal with shared state? Functional Imperative OOP
  • 47. Pure functions reduce need for most OOP design patterns VS ‘Cause who doesn’t want to know about your MVVMVMVMMVCCC singleton factory observer adapter thingy? Functional Imperative OOP
  • 49. Single Input Single Output Func A B
  • 50. Functions Of This Sort Can Be Things Func A B
  • 51. Functions Of This Sort Can Be Things Func A B
  • 56. Function Composition Is The New Old Injection
  • 57. // Goal: Allow brewing coffee with different techniques and beans // Types // Beans => Grind // Grind => Coffee Compose to get: Beans => Coffee trait Beans trait Grind trait Coffee case class ColumbianBeans() extends Beans def brew(grind: Beans => Grind)(brew: Grind => Coffee): Beans => Coffee = { brew compose grind } def burrGrind(beans: Beans): Grind = ??? def blendGrind(beans: Beans): Grind = ??? def pourOver(grind: Grind): Coffee = ??? def drip(grind: Grind): Coffee = ??? def frenchPress(grind: Grind): Coffee = ??? val pourOverWithBurrGrind = brew(burrGrind)(pourOver) val myFavoriteBeans = ColumbianBeans() val myFavoriteCoffee = pourOverWithBurrGrind(myFavoriteBeans) // TIME TO WAKE UP!!!
  • 58. // Goal: Allow brewing coffee with different techniques and beans // Types // Beans => Grind // Grind => Coffee Compose to get: Beans => Coffee trait Beans trait Grind trait Coffee case class ColumbianBeans() extends Beans def brew(grind: Beans => Grind)(brew: Grind => Coffee): Beans => Coffee = { brew compose grind } def burrGrind(beans: Beans): Grind = ??? def blendGrind(beans: Beans): Grind = ??? def pourOver(grind: Grind): Coffee = ??? def drip(grind: Grind): Coffee = ??? def frenchPress(grind: Grind): Coffee = ??? val pourOverWithBurrGrind = brew(burrGrind)(pourOver) val myFavoriteBeans = ColumbianBeans() val myFavoriteCoffee = pourOverWithBurrGrind(myFavoriteBeans) // TIME TO WAKE UP!!! brew is a higher order function
  • 60. // Imagine how many combinations we can use this code // Imagine how easy testing is object Person { def drink(coffee: Coffee): AwakePerson = ??? } def wakeSomeone(makeIt: () => Coffee): AwakePerson = { Person.drink(makeIt()) } Anything that makes coffee
  • 62. // Which right triangle that has integers for all sides and all sides equal to // or smaller than 10 has a perimeter of 24? def rightTriangle: Seq[Int] = for { c <- 1 to 10 b <- 1 to c a <- 1 to b if ((Math.pow(a,2) + Math.pow(b,2) == Math.pow(c,2)) && (a + b + c == 24)) } yield (a, b, c) rightTriangle Answer: [6, 8, 10] It’s Not About the How but the What
  • 64. f(x) = 3/x Int => Double
  • 65. f(x) = 3/x 1 => 3/1 => 3.0 2 => 3/2 => 1.5 0 => 3/0 => ???
  • 66. f(x) = 3/x 1 => 3/1 => 3.0 2 => 3/2 => 1.5 0 => 3/0 => ??? AH, PICKLES!
  • 67. f(x) = 3/x 1 => 3/1 => 3.0 2 => 3/2 => 1.5 0 => 3/0 => ??? THROW AN EXCEPTION?
  • 68. f(x) = 3/x Int => Double 0 => 3/0 => ??? THROW AN EXCEPTION? THEN THIS IS A LIE!
  • 69. f(x) = 3/x Int => Double 0 => 3/0 => ??? THROW AN EXCEPTION? THEN THIS IS A LIE! Are You A Liar?
  • 70. But Functions Are Types! Types Don’t Have to Be Open to All Circumstances
  • 71. f(x) = 3/x NonZeroInt => Double Just Became Constrained And Self-Documented
  • 72. f(x) = 3/x Zero Not Allowed BY NonZeroInt -1 => 3/-1 => -3 1 => 3/1 => 3 2 => 3/2 => 1.5
  • 73. Static Types Just Became Enforced Domain Documentation
  • 74.
  • 75. We Sometimes Refer to Our Programs as an “Algebra”
  • 76. If It Compiles, It Probably Works
  • 78. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } }
  • 79. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Yeah, I know this is bad. But you know you’ve seen this before. Hang with me.
  • 80. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Pyramid of Doom
  • 81. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Pyramid of Doom The Only Lines Doing Work That’s only 20%!
  • 82. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Pyramid of Doom Look at all those nulls! Half this code is null checking. Nulls are a serious code smell!
  • 83. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Pyramid of Doom Look at all those nulls! Half this code is null checking. Nulls are a serious code smell! Null Pointer Exceptions Waiting to Happen
  • 84. def foo(): Thing = { val x = DoAThing() if (x != null) { val y = DoAnotherThing(x) if (y != null) { val z = DoYetAnotherThing(y) if (z != null) { return z } else { return null } } else { return null } } else { return null } } Pyramid of Doom Throw in async operations, we can create the same pyramid with nested callbacks.
  • 85. FP to the Rescue!
  • 86. def foo(): Option[Int] = { for { x <- DoAThing() y <- DoAnotherThing(x) z <- DoYetAnotherThing(y) } yield z }
  • 87. def foo(): Option[Int] = { for { x <- DoAThing() y <- DoAnotherThing(x) z <- DoYetAnotherThing(y) } yield z } Every line is significant
  • 88. def foo(): Option[Int] = { for { x <- DoAThing() y <- DoAnotherThing(x) z <- DoYetAnotherThing(y) } yield z } Every line is significant
  • 90. Application Becomes a Collection of Pure Functions
  • 91. Just Fold the Collection
  • 93.
  • 94. Monads The Key to a Lot of Awesome
  • 98. Monad Type with a Functor + FlatMap
  • 99. State
  • 101. State The box is a “monad”
  • 102. State Think of monad as collection of at most one item
  • 103. State You can do collection things to monads Operation creates new monad of same type but new state .map(...) .flatMap(...) .fold(...) .filter(...) .collect(...) .head
  • 104. State If Monad is “empty”, applied functions are ignored .map(...) .flatMap(...) .fold(...) .filter(...) .collect(...) .head
  • 105. def foo(): Option[Int] = Some(100) val myFoo = foo() val result = myFoo .map(i => i + 100) .map(i => i.toString) .map(i => i + " is a bigger number") .getOrElse("didn't do anything") // result = "200 is a bigger number"
  • 106. def foo(): Option[Int] = None val myFoo = foo() val result = myFoo .map(i => i + 100) .map(i => i.toString) .map(i => i + " is a bigger number") .getOrElse("didn't do anything") // result = "didn’t do anything" Executed but not applied
  • 107. Monads are so much more but let’s keep it simple for now… ...another talk to come
  • 109. Trends in Functional Programming Data Processing Streaming Parallelization Machine Learning Heavy Computation Attracting Talent
  • 111.
  • 113. Natural way to migrate To Functional Programming?
  • 114. thanks! Any questions? You can find me at @ProfParmer

Editor's Notes

  1. Good morning. My name is Jordan. I’m a software engineer for a company in OKC called “Oseberg” where I lead the Data Platform team. We use a lot of FP at Oseberg. In fact, all of our server-side code is FP using a language called Scala. How many of you have used functional programming professionally? As a hobby? A strict functional programming language?
  2. Get a raise of hands for who has done functional programming before.
  3. Describe the paradigm phases over the years (procedural => oop => functional) Fascination of video games as child (not necessarily gaming itself but the building aspect) Story of dad buying my first programming book (show next slide with QBasic book)
  4. I read this book cover-to-cover. Was fascinated. Felt like I knew a secret code or language that few others knew. I think that is a feeling many of us in this room probably felt at one time or another. The love of the tinkering. The love of the hacking around. The love of building something out of logic. Infinite loops, beeping ASCII codes, etc.
  5. When we first learn programming, we are concerned about syntax. How do I enter the correct codes, symbols, and keywords to appease the wrath of the compiler and have our little code run? Next, we are concerned with how to model things. What is the best way to represent this logical concept in the real world? Next, how do we have our models interact with each other? Next, how do we work in a team with other people writing code? Next, how do we have systems talk to each other? … The further we go, the more evolved the problems we are solving. In today’s world, we now find ourselves processing Big Data. Massive volumes of data, many CPU cores, distributed computing clusters, and a high degree of parallelism. The problems we are solving today are not the problems we were solving yesterday.
  6. That is why our field is about the pursuit of building. We are builders first and technologists second. How do we build things? And this means we should always be learning how to build in new ways.
  7. Ask the audience.
  8. Ask the audience.
  9. Ask the audience.
  10. Now this doesn’t mean we don’t have to learn new terminology. When we learned algebra, we could no longer constrain ourselves to basic addition and subtraction. Had to learn new phrases. When we learned OOP, we had to learn about classes, objects, encapsulation, inheritance, etc. New lingo for different techniques. Same with FP. You are going to have to learn about referential integrity, functors, monoids, monads, typeclasses, etc.
  11. What if in algebra class your teacher docked you points because in reality, usage of ‘x’ mutated its state so that it wasn’t constant within the use of the function f() and the answer changes? You’d cry fowl! But don’t we do this kind of stuff all the time? When we were first becoming programmers, this is the kind of habits we were taught. Changing state.
  12. What if in algebra class your teacher docked you points because in reality, usage of ‘x’ mutated its state so that it wasn’t constant within the use of the function f() and the answer changes? You’d cry fowl! But don’t we do this kind of stuff all the time? When we were first becoming programmers, this is the kind of habits we were taught. Changing state.
  13. Will this work for all inputs? How might we code this up?
  14. Will this work for all inputs? How might we code this up?
  15. Will this work for all inputs? How might we code this up?
  16. Will this work for all inputs? How might we code this up?
  17. Will this work for all inputs? How might we code this up?
  18. Will this work for all inputs? How might we code this up?
  19. Will this work for all inputs? How might we code this up?
  20. Will this work for all inputs? How might we code this up?