SlideShare a Scribd company logo
Functional programming
Functional Programming (Languages) FP(L)
Agenda
● Why functional programming?
● Functional Programming
● Benefits of FP
● Concurrency
● How to program functionally
In the beginning there was ...
Lambda calculus
● Theoretical framework
● Describes functions and their evaluations
● Basis of almost all FPL today
Introduction to functional programming
Why is functional programming important?
The benefits of functional
programming
● succinct, concise and understandable code
● offers different programming perspective
● FP becoming more accessible
● concurrency / parallelism without tears
Why FP is important?
Row 1 Row 2 Row 3 Row 4
0
2
4
6
8
10
12
Column 1
Column 2
Column 3
Wirth's Law
Software is getting slower more rapidly than
hardware becomes faster
FP != FPL
Introduction to functional programming
If all of these are functional programming
(languages), isn't it overwhelming and difficult?
Not really. But why?
What is functional programming?
What is a functional programming language?
Functional programming is just a style
Programming styles
Declarative vs. Imperative programming
What? vs. How?
What is object-oriented
programming?
● Reuse code via classes
● Eliminate bugs
– Encapsulation
– Data hiding
Functional programming
● Reuse code via function composition
● Eliminate bugs
– Immutability
– Pure functions
Introduction to functional programming
Introduction to functional programming
Programs as functions
● No loops but recursion
● Minimise number of variables
● no assignment operation
● only constants, parameters and values
Introduction to functional programming
The dark side
too much recursion can overflow the stack
(without tail-call optimization)
high memory consumptions from creating so
many objects
No variables, no assignment – No
problem
● No notion of the internal state of a function
● referential transparency
● value semantics
Referential Transparency
An expression is said to be referentially
transparent if it can be replaced with its value
without changing the behaviour of a program
25 == 5 * 5
Value semantics
It means for an object that only its value counts,
not its identity
Coping an object doesn't change the behaviour
of your program
Pure Function
● Always evaluates to the same result
● Evaluation does not cause side effects
Introduction to functional programming
What does it mean in practice?
No bugs due to nasty side effects
Mutable state
Map<Person, String> map = …
Person p = new Person('John');
map.put(p, "Hey, there!");
p.setName("Dark Lord");
Oops!
map.get(p); // => null
Immutability
“Object-oriented programming makes code
understandable by encapsulating moving parts.
Functional programming makes code
understandable by minimizing moving parts.”
— Michael Feathers, author of Working with Legacy Code, via
Twitter
● Data once created are never changed
● Need to “update” your data? – create new one
● Safe to share data between threads
HOF
Higher order function
– Takes one or more functions as an input
and/or
– Outputs function
Ruby
● Map
● Each
● Reduce
● Inject
● Select
Ruby
def twice(f, x)
f.call(f.call(x))
end
f1 = ->(x){ x / 3 }
print twice(f1, 9) # 1
JavaScript
function twice(f, x){
return f(f(x));
}
function f(x){
return x*3;
}
twice(f, 7); // 63
Higher-order functions start to shine when you
need to compose functions
JavaScript
function filter(array, test) {
var passed = [];
for (var i = 0; i < array.length; i++) {
if (test(array[i]))
passed.push(array[i]);
}
return passed;
}
function map(array, transform) {
var mapped = [];
for (var i = 0; i < array.length; i++)
mapped.push(transform(array[i]));
return mapped;
}
function average(array) {
function plus(a, b) { return a + b; }
return array.reduce(plus) / array.length;
}
function age(p) { return p.died - p.born; }
function male(p) { return p.sex == "m"; }
average(ancestry.filter(male).map(age))
Lazy evaluation
● Delaying evaluation of an expression
● Reduction of memory footprint as values are created
when needed not when they are declared
● Potential to construct infinite data structures
● Requires pure functions as order of operations becomes
indeterminate
Lazy Fibonacci
● Haskell
● Infinite list
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
● Take only required Fibonacci numbers
Memoization
● Storing the result of function calls
● Returning the cached result
● Trade-off between function time cost and space
cost
● Run-time not compile-time optimisation
Introduction to functional programming
You can program functionally in a variety of
programming languages
A functional language provides 'sane' defaults
Why is FP important? (again)
● formal provability
● code comprehension modularity
● unit testing and debugging
● automation
● maintainability
● hot code deployment
Provability
simpler execution model makes program proving
easier both for computers and humans
Code comprehension
easier to understand and reason about because
you don't have to worry about what is happening
behind the scenes
Modularity
modular design brings with it great productivity
improvements. Small modules can be coded
quickly and easily and have a greater chance of
re-use which leads to faster development of
programs.
Additionally, the modules can be tested
independently, thus helping to reduce the time
spent unit testing and debugging
Testing, debugging & automation
● PF is much easier to reason about
● specific arguments always return specific results
● problems are easy to troubleshoot/duplicate because no
state or external dependencies
● automation is simplified due to lack of environmental,
state or configuration concerns
Maintainability
easier to maintain as you don't have to worry
about accidentally changing anything outside a
given function
Concurrency
Compilers think, too
Introduction to functional programming
Introduction to functional programming
In the purely functional world, the compiler does
not need proof of non-interference. It is built into
the programming language.
Implicit parallelism is free in functional
programming languages.
Sadly, this story is naïve and unrealistic and yet it
contains the key to a parallel world.
In the imperative world mutation creates too few
opportunities for automatic parallel execution
In the functional world a lack of dependencies
means too many opportunities for automatic
parallel execution.
The imperative world will see explicit parallel
programming and the big battle against race
condition bugs.
The functional world will provide explicit parallel
programming with fewer race conditions.
Introduction to functional programming
How to program functionally?
Avoid side-effects
● do not modify variables passed to functions
● do not modify any global variables
● Use higher-order functions
● Limit variable assignment
● Same input → same output
● Don't share state
Does order matter?
Order can be a side effect
● Use immutable data
● Lazy evaluation
Decompose problems into functions
(whenever you can)
Questions
Any questions?
Slack #fp
Resources
http://eloquentjavascript.net/05_higher_order.html
http://www.sitepoint.com/functional-programming-tec
hniques-with-ruby-part-ii/
http://www.functionalprogramming.com/history.html
http://www.cs.kent.ac.uk/people/staff/dat/miranda/w
hyfp90.pdf

More Related Content

What's hot (20)

PDF
Introduction of Functional Programming
☁️ Mikhail Shilkov
 
PDF
Intro to functional programming
Assaf Gannon
 
PPT
Introduction to Functional Programming in JavaScript
tmont
 
PDF
Functional Programming in Scala: Notes
Roberto Casadei
 
PPT
Scala functions
Knoldus Inc.
 
PDF
Functional Programming in Scala
Bassam Abd El Hameed
 
PDF
Introduction to programming in scala
Amuhinda Hungai
 
PDF
Functional programming in Scala
Damian Jureczko
 
PDF
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
PDF
Ankara Jug - Practical Functional Programming with Scala
Ensar Basri Kahveci
 
PDF
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
 
PDF
Functional Programming with JavaScript
WebF
 
PPT
An Overview Of Python With Functional Programming
Adam Getchell
 
PDF
An Introduction to Functional Programming - DeveloperUG - 20140311
Andreas Pauley
 
PDF
Functional go
Geison Goes
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
PPTX
Functional programming in JavaScript
Joseph Smith
 
PDF
Introduction to Functional Programming with Scala
pramode_ce
 
PPTX
An Introduction to Functional Programming with Javascript
Doug Sparling
 
PDF
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 
Introduction of Functional Programming
☁️ Mikhail Shilkov
 
Intro to functional programming
Assaf Gannon
 
Introduction to Functional Programming in JavaScript
tmont
 
Functional Programming in Scala: Notes
Roberto Casadei
 
Scala functions
Knoldus Inc.
 
Functional Programming in Scala
Bassam Abd El Hameed
 
Introduction to programming in scala
Amuhinda Hungai
 
Functional programming in Scala
Damian Jureczko
 
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
Ankara Jug - Practical Functional Programming with Scala
Ensar Basri Kahveci
 
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
 
Functional Programming with JavaScript
WebF
 
An Overview Of Python With Functional Programming
Adam Getchell
 
An Introduction to Functional Programming - DeveloperUG - 20140311
Andreas Pauley
 
Functional go
Geison Goes
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Functional programming in JavaScript
Joseph Smith
 
Introduction to Functional Programming with Scala
pramode_ce
 
An Introduction to Functional Programming with Javascript
Doug Sparling
 
Functional Programming Patterns (BuildStuff '14)
Scott Wlaschin
 

Viewers also liked (10)

PPTX
San cristóbal ikastetxea
JoneAbasolo
 
PPT
бекше даурен++книги+дети +взрослые люди
Dauren DaurenKz
 
PPT
Ngai Language Barrier in Disaster Planning
Gordon Ngai
 
PDF
Inf1 p1 b_exc7_celina_valenzuela_mitzi_garcia
CelinaValenzuela
 
PPTX
Patologia Quirurgica Parte 2 Trauma
El Cientifico De La Nasa
 
PDF
Biotechnology
lauracarbonellherrero
 
DOC
AS cwk guide 10 steps
DB3igs
 
PDF
Data Center Architecture Trends
Panduit
 
San cristóbal ikastetxea
JoneAbasolo
 
бекше даурен++книги+дети +взрослые люди
Dauren DaurenKz
 
Ngai Language Barrier in Disaster Planning
Gordon Ngai
 
Inf1 p1 b_exc7_celina_valenzuela_mitzi_garcia
CelinaValenzuela
 
Patologia Quirurgica Parte 2 Trauma
El Cientifico De La Nasa
 
Biotechnology
lauracarbonellherrero
 
AS cwk guide 10 steps
DB3igs
 
Data Center Architecture Trends
Panduit
 
Ad

Similar to Introduction to functional programming (20)

PPTX
Why functional programming in C# & F#
Riccardo Terrell
 
PPTX
Introduction to Functional Programming
Dave Fancher
 
PPTX
Functional programming
Prateek Jain
 
PPTX
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
PPTX
When life gives you functions make functional programs!
Aaron Levin
 
PDF
Functional programming
Hideshi Ogoshi
 
PPTX
Functional programming
Prashant Kalkar
 
PPTX
Exploring the Real Power of Functional Programming
Knoldus Inc.
 
PPTX
Functional programming
PiumiPerera7
 
PPTX
The joy of functional programming
Steve Zhang
 
PDF
Introduction to functional programming
Thang Mai
 
PDF
Intro to Functional Programming @ Scala Montreal
felixtrepanier
 
PDF
379008-rc217-functionalprogramming
Luis Atencio
 
PDF
10 good reasons to invest your time in FP
Joel Corrêa
 
PDF
Fp for the oo programmer
Shawn Button
 
PDF
Functional programming is the most extreme programming
samthemonad
 
PDF
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
PDF
Functional programming
OpenAgile Romania
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
Why functional programming in C# & F#
Riccardo Terrell
 
Introduction to Functional Programming
Dave Fancher
 
Functional programming
Prateek Jain
 
From Imperative to Functional Programming (for Absolute Beginners)
Alex Bunardzic
 
When life gives you functions make functional programs!
Aaron Levin
 
Functional programming
Hideshi Ogoshi
 
Functional programming
Prashant Kalkar
 
Exploring the Real Power of Functional Programming
Knoldus Inc.
 
Functional programming
PiumiPerera7
 
The joy of functional programming
Steve Zhang
 
Introduction to functional programming
Thang Mai
 
Intro to Functional Programming @ Scala Montreal
felixtrepanier
 
379008-rc217-functionalprogramming
Luis Atencio
 
10 good reasons to invest your time in FP
Joel Corrêa
 
Fp for the oo programmer
Shawn Button
 
Functional programming is the most extreme programming
samthemonad
 
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Functional programming
OpenAgile Romania
 
Intro f# functional_programming
Mauro Ghiani
 
Ad

Recently uploaded (20)

PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 

Introduction to functional programming