SlideShare a Scribd company logo
Functional Programming:
Why and What
Naveen Muguda
Software Development: Empathy
Business
Human
Infrastructure
Time
Breakfast?
Awareness test
Phone number test
Mental calculation test
• 39,865 / 17
pronunciation
• Hippopotomonstrosesquippedaliophobia
Humans
• can’t observe much
• can’t store much in short term memory
• can’t solve moderately complex problems
• are not detail oriented
=> we cant handle “more”
we need simplicity
Word origin
• simplex (adj.)
• "characterized by a single part," 1590s, from Latin simplex "single,
simple, plain, unmixed, uncompounded," literally "onefold"
Two poses
Word origin
• From late 14c. as "of or pertaining to one's family."
Simplicity vs Familiarity
Familiarity
Simplicity
Eagle pose
Standing Forward Bend pose
Standing Forward Bend pose(me)
semiformally
• simplicity = g(object)
• familiarity = h(object, person)
Simplicity vs Familiarity
Simplicity
Procedural
Object oriented
Functional
Familiarity
statement vs expression
• stack.push(..)
• stack.pop()
statements vs expressions
Egg egg ;
BoiledEgg boiledEgg = null;
PeeledEgg peeledEgg = null;
boilWater();
addEgg();
peel();
• implies a specific order of evaluation,
• which means that a statement cannot be understood without looking
at prior statements.
Expression oriented Programming
• Egg egg = ;
• BoiledEgg boiledEgg = boil(egg)
• BoiledAndPeeledEgg boiledAndPeeledEgg = peel(boiledEgg)
Expression oriented Programming
• Expressions promote local reasoning
Functional Programming
• Expression oriented programming
Which version is the best?
• for(int i = 0; i < a.length, i++)
vs
• for(int car = 0; car < a.length, car++)
vs
• for(int index = 0; index < a.length; index++)
Mirroring
Low Representational Gap
• The ”concept” being modelled should be mirrored by its software
representation.
• Keep the distance between concept and its representation low
functions
• Methods that always return the same result for the same input(s)
functions, functions every where
• sort
• search
• tree/graph traversal
• statistics
Real world constructs modelled as functions
• taxes
• scores
• search results
• data transformations
Expressions, Not functions
• stack.pop()
• Random.nextInt()
• time()
statements
expressions
functions
Functional Programming
• Expression oriented programming
• Functions as first class construct
Identify problems with this code
<A extends Comparable<A>> A max(A first, A second) {
return first.compareTo(second) > 0 ? first : second;
}
First pass
<A extends Comparable<A>> A max(A first, A second) {
synchronized(first) {
synchronized(second) {
return first.compareTo(second) > 0 ? first : second;
}
}
}
immutability
<A extends Comparable<A>> A max(A first, A second) {
return first.compareTo(second) > 0 ? first : second;
}
functions
• always return the same result for the same input
• doesn’t read mutable state
• doesn’t write to mutable state
Error prone
Functional Programming
• Expression oriented programming
• Functions as first class construct
• Programming with values
Another phone number
• 9731-691-691
• “I ran 5.23 kms today”
Composition
• backbone of (good) thinking/communication
• Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
Function<Integer, Integer> h = g.compose(f);
composition
who/what are the underlined
• The weather was great in Baratang
• Jigme singye wangchuck was loved by his subjects
composability
• Locks don’t compose
• Functions compose
Functional Programming
• Expression oriented programming
• Functions as first class construct
• Programming with values
• Function composition as a first class mechanism
Communication|thinking test
• How do you get to the gym?
• How do you make a lemonade?
• Explain quicksort
Essence over ceremony
Guess what this function does
• public static int m(int i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
Inversions
int sum = 0;
for(Integer element:list)
sum += element;
int max = MIN_VALUE;
for(Integer element:list)
max = (max > element) ? max: element
Functions as values | Higher order Functions
• <U> U fold (U zero, BiFunction <U, T, U> combine);
• List<Integer> list = List.of(4, 2, 1, 6);
list.fold(0, (x, y) -> x + y); // sum
list.fold(MIN_VALUE, (x, y) -> (x > y)? x : y);//max
fold
• Search
• Sort
• Filter
• Reverse
• Count
• ……
for(String string: strings) {
if (string == null)
continue;
for(String word: split(string,"s" )){
if(word.length() > big.length())
big = word;
}
}
return strings
.filter(Objects::nonNull)
.flatMap(s -> split(s, "s"))
.fold("", (x, y) -> (x.length() > y.length()) ? x : y);
Functional Programming
• Expression oriented programming
• Functions as first class construct
• Programming with values
• Function composition
• Higher Order functions
Inversions
Play
Hello, World
IPL team isSpinner
Kohli RCB no
Chahal RCB yes
Kuldeep KKR yes
Pujara null no
Team Won IPL in
RCB null
KKR 2012, 2014
Different containers, different behaviors
• Optional.of(“pujara”). map(getIplTeam).map(firstWonIn) =>
Optional.empty
• Optional.of(“kohli”). map(getIplTeam).map(firstWonIn) =>
Optional.empty
• Set.of(“kohli”, “chahal”, “kulcha”).map(getIplTeam) => Set.of(“RCB”,
“KKR”)
• Set.of(“kohli”, “chahal”, “kulcha”).map(getIplTeam).flatMap(wonIn) =>
Set.of(2012, 2014)
• List.of(“kohli”, “chahal”, “kulcha”).map(getIplTeam) => List.of(“RCB”,
“RCB”, “KKR”)
Optional
List
Set
map
filter
flatmap
fold
7constructs vs 12
Containers
Optional Null Safety
List Ordered collection
Set Unique collection
Promise Async Programming
Task Computation
Functional Programming
• Expression oriented programming
• Functions as first class construct
• Programming with values
• Function composition
• Higher Order functions
• Behavior rich containers
Structure preserving operations
Set.of(“kohli”, “chahal”, “kulcha”)
.filter(isSpinner)
.map(getIplTeam)
.flatMap(wonIn) => Set.of(2012, 2014)
Functional Programming
• Expression oriented programming
• Functions as first class construct
• Programming with values
• Function composition
• Higher Order functions
• Behavior rich containers
• Structure preserving operations
Parallel fold
Procedural OO Functional
Reuse Low Low High
Time to Closure High Medium Low
Testability Low Medium High
Development time High Medium Low
# of Artifacts Low Medium HIgh

More Related Content

What's hot

Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
Bryan O'Sullivan
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
jomerson remorosa
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
Bryan O'Sullivan
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
Bryan O'Sullivan
 

What's hot (20)

Functional programming with F#
Functional programming with F#Functional programming with F#
Functional programming with F#
 
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 
FYP Final Presentation
FYP Final PresentationFYP Final Presentation
FYP Final Presentation
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Standard data-types-in-py
Standard data-types-in-pyStandard data-types-in-py
Standard data-types-in-py
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Introduction to haskell
Introduction to haskellIntroduction to haskell
Introduction to haskell
 
Introducing: A Complete Algebra of Data
Introducing: A Complete Algebra of DataIntroducing: A Complete Algebra of Data
Introducing: A Complete Algebra of Data
 
Scala collection
Scala collectionScala collection
Scala collection
 
Real World Haskell: Lecture 5
Real World Haskell: Lecture 5Real World Haskell: Lecture 5
Real World Haskell: Lecture 5
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!Beginning Haskell, Dive In, Its Not That Scary!
Beginning Haskell, Dive In, Its Not That Scary!
 
Advanced data structures slide 1 2
Advanced data structures slide 1 2Advanced data structures slide 1 2
Advanced data structures slide 1 2
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Real World Haskell: Lecture 2
Real World Haskell: Lecture 2Real World Haskell: Lecture 2
Real World Haskell: Lecture 2
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Monads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy DyagilevMonads and Monoids by Oleksiy Dyagilev
Monads and Monoids by Oleksiy Dyagilev
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 

Similar to Fp

Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
Sergio Gil
 

Similar to Fp (20)

Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
app4.pptx
app4.pptxapp4.pptx
app4.pptx
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Functional programming
Functional programming Functional programming
Functional programming
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Functional Programming with Groovy
Functional Programming with GroovyFunctional Programming with Groovy
Functional Programming with Groovy
 
Class 7a: Functions
Class 7a: FunctionsClass 7a: Functions
Class 7a: Functions
 
Towards Programming Languages for Reasoning.pptx
Towards Programming Languages for Reasoning.pptxTowards Programming Languages for Reasoning.pptx
Towards Programming Languages for Reasoning.pptx
 
Erlang/OTP for Rubyists
Erlang/OTP for RubyistsErlang/OTP for Rubyists
Erlang/OTP for Rubyists
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 
Python basic
Python basicPython basic
Python basic
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 

More from Naveenkumar Muguda

More from Naveenkumar Muguda (11)

Ads quality
Ads qualityAds quality
Ads quality
 
Components: An overlooked abstraction
Components: An overlooked abstractionComponents: An overlooked abstraction
Components: An overlooked abstraction
 
Powerful software linkedin
Powerful software linkedinPowerful software linkedin
Powerful software linkedin
 
Programming in the large
Programming in the largeProgramming in the large
Programming in the large
 
Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory
 
Invariants & inversions
Invariants & inversionsInvariants & inversions
Invariants & inversions
 
Functional Programming, simplified
Functional Programming, simplifiedFunctional Programming, simplified
Functional Programming, simplified
 
Software Development: Beyond Training wheels
Software Development: Beyond Training wheelsSoftware Development: Beyond Training wheels
Software Development: Beyond Training wheels
 
Log* with Cassandra
Log* with CassandraLog* with Cassandra
Log* with Cassandra
 
Refactoring et al
Refactoring et alRefactoring et al
Refactoring et al
 
System design
System designSystem design
System design
 

Recently uploaded

Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
ssuser9bd3ba
 

Recently uploaded (20)

Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
CME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional ElectiveCME397 Surface Engineering- Professional Elective
CME397 Surface Engineering- Professional Elective
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Explosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdfExplosives Industry manufacturing process.pdf
Explosives Industry manufacturing process.pdf
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
LIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.pptLIGA(E)11111111111111111111111111111111111111111.ppt
LIGA(E)11111111111111111111111111111111111111111.ppt
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge2024 DevOps Pro Europe - Growing at the edge
2024 DevOps Pro Europe - Growing at the edge
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
shape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptxshape functions of 1D and 2 D rectangular elements.pptx
shape functions of 1D and 2 D rectangular elements.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Introduction to Casting Processes in Manufacturing
Introduction to Casting Processes in ManufacturingIntroduction to Casting Processes in Manufacturing
Introduction to Casting Processes in Manufacturing
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 

Fp