SlideShare a Scribd company logo
Functional
Programming
@bruno_lui
Imperative
programming paradigm
is about . . .
modifying mutable variables
using assignments
and control structures such
as if, then, else, lops, break,
continue, return
Functional
programming paradigm
means ...
modifying mutable variables
assignments
programming without
and other imperative
control structures
programming focusing on
the functions
functions ARE values that
can be produced, consumed
and composed
Principles
and
Concepts
Immutable Data
Functional programs should be immutable,
which means you would simply create new
data structures
instead of modifying ones that already exist.
Referential transparency
Functional programs should perform every task
as if for the first time, with no knowledge of what
may or may not have happened earlier in the
program’s execution and without side effects.
Function as first-class citizens
functions can be defined anywhere
they can be passed as parameters to functions and
returned as results
there exists a set of operators to compose functions
• A number can be stored in a variable and so can a function: 

var fortytwo = function() { return 42 };
• A number can be stored in an array slot and so can a function: 

var fortytwos = [42, function() { return 42 }];
• A number can be stored in an object field and so can a function: 

var fortytwos = {number: 42, fun: function() { return 42 }}; 

• A number can be created as needed and so can a function: 

42 + (function() { return 42 })(); //=> 84
• A number can be passed to a function and so can a function: 

function weirdAdd(n, f) { return n + f() } weirdAdd(42, function() { return 42 }); 

//=> 84 

• A number can be returned from a function and so can a function:


return 42;

return function() { return 42 }; 

Composing Functions
In functional programming, you’re always
looking for simple, repeatable actions to be
abstracted out into a function.
We can then build more complex features by
calling these functions in sequence
"Functional programming is the use of functions that
transform values into units of abstraction,
subsequently used to build software systems."
code examples
Give a 10% discount to all products of the chart above $30
Show the total value of products and discount
Imperative
Double valorTotal = 0d;
Double descontoTotal = 0d;
for (Produto produto : produtos) {
valorTotal += produto.getValor();
if (produto.getValor() > 30.00) {
descontoTotal += produto.getValor() * 0.1;
}
}
valorTotal -= descontoTotal;
functional
val valores = produtos map (_.valor)
val valorTotal = valores reduce
((total, valor) => total + valor)
val descontoTotal = valores filter
(_ > 30.00) map
(_ * 0.10) reduce
((total, desconto) => total + desconto)
val total = valorTotal - descontoTotal
code examples
Write a program to build a lyric sheet for
the song “99 bottles of beer"
X bottles of beer on the wall
X bottles of beer
Take one down, pass it around
X-1 bottles of beer on the wall
No more bottles of beer on the wall
Imperative
var lyrics = []; 

for (var bottles = 99; bottles > 0; bottles--) {
lyrics.push(bottles + " bottles of beer on the wall”);
lyrics.push(bottles + " bottles of beer");
lyrics.push("Take one down, pass it around");
if (bottles > 1) {

lyrics.push((bottles - 1) + " bottles of beer on the wall.");
} else { 

lyrics.push("No more bottles of beer on the wall!”);
}
}
functional
function lyricSegment(n) {
return _.chain([])
.push(n + " bottles of beer on the wall")
.push(n + " bottles of beer”)
.push("Take one down, pass it around")
.tap(function(lyrics) {
if (n > 1)

lyrics.push((n - 1) + " bottles of beer on the wall.");
else
lyrics.push("No more bottles of beer on the wall!");
})
.value();
}


functional
lyricSegment(9);
//=> ["9 bottles of beer on the wall",
// "9 bottles of beer",
// "Take one down, pass it around",
// "8 bottles of beer on the wall."]
functional
function song(start, end, lyricGen) {
return _.reduce(_.range(start,end,-1),
function(acc,n) {

return acc.concat(lyricGen(n));
}, []);
}
And using it is as simple as:
song(99, 0, lyricSegment);
//=> ["99 bottles of beer on the wall",
// ...
// "No more bottles of beer on the wall!"]
1. All of your functions must accept at least one argument.
2. All of your functions must return data or another function.
3. No loops
3 simple rules to avoid
imperative habits
conclusions
data abstractions
composing functions
declarative programming
concise code
Coursera: Functional Programming Principles in Scala
Book: Functional Javascript, Michael Fogus
http://www.smashingmagazine.com/2014/07/02/dont-be-scared-of-functional-
programming/
References
Thank you
@bruno_lui

More Related Content

What's hot (19)

PPTX
C Programming Language Part 7
Rumman Ansari
 
PPSX
C programming function
argusacademy
 
PPTX
Lessons learned from functional programming
BryceLohr
 
PPSX
Functions in c
Innovative
 
PPTX
Call by value
Dharani G
 
PPTX
Call by value or call by reference in C++
Sachin Yadav
 
PPTX
parameter passing in c#
khush_boo31
 
PPTX
Functions in C
Princy Nelson
 
PPT
Functions in C++
Nikhil Pandit
 
PPTX
Function (rule in programming)
Unviersity of balochistan quetta
 
PPT
Pre defined Functions in C
Prabhu Govind
 
PPTX
functions of C++
tarandeep_kaur
 
PPTX
C Programming Language Part 6
Rumman Ansari
 
PPTX
C++ lecture 03
HNDE Labuduwa Galle
 
PPTX
How c program execute in c program
Rumman Ansari
 
PPTX
C function presentation
Touhidul Shawan
 
PPT
16717 functions in C++
LPU
 
PPT
Functions in C++
Sachin Sharma
 
PPT
Single row functions
Soumyajit Dutta
 
C Programming Language Part 7
Rumman Ansari
 
C programming function
argusacademy
 
Lessons learned from functional programming
BryceLohr
 
Functions in c
Innovative
 
Call by value
Dharani G
 
Call by value or call by reference in C++
Sachin Yadav
 
parameter passing in c#
khush_boo31
 
Functions in C
Princy Nelson
 
Functions in C++
Nikhil Pandit
 
Function (rule in programming)
Unviersity of balochistan quetta
 
Pre defined Functions in C
Prabhu Govind
 
functions of C++
tarandeep_kaur
 
C Programming Language Part 6
Rumman Ansari
 
C++ lecture 03
HNDE Labuduwa Galle
 
How c program execute in c program
Rumman Ansari
 
C function presentation
Touhidul Shawan
 
16717 functions in C++
LPU
 
Functions in C++
Sachin Sharma
 
Single row functions
Soumyajit Dutta
 

Similar to Functional Programming (20)

PPTX
Functional programming in JavaScript
Joseph Smith
 
PDF
379008-rc217-functionalprogramming
Luis Atencio
 
ODP
Functional programming
S M Asaduzzaman
 
PDF
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
PPTX
Functional Programming in Swift
Saugat Gautam
 
PPTX
When life gives you functions make functional programs!
Aaron Levin
 
PPTX
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
PDF
Functional Programming with JavaScript
WebF
 
PPTX
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
PPTX
Declarative JavaScript concepts and implemetation
Om Shankar
 
PDF
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
PPTX
Functionnal programming
AndryRajohnson
 
PPTX
Functional programming
Nyarai Tinashe Gomiwa
 
PPTX
Functional Programming
SovTech (Scrums.com)
 
PDF
Intro to functional programming - Confoo
felixtrepanier
 
PDF
Functional Programming Principles & Patterns
zupzup.org
 
PPTX
Introduction to Functional programming
Ny Fanilo Andrianjafy, B.Eng.
 
PDF
Introduction to functional programming
Konrad Szydlo
 
PDF
Functional programming
Hideshi Ogoshi
 
Functional programming in JavaScript
Joseph Smith
 
379008-rc217-functionalprogramming
Luis Atencio
 
Functional programming
S M Asaduzzaman
 
"Functional Programming in a Nutshell" by Adityo Pratomo (Froyo Framework)
Tech in Asia ID
 
Functional JavaScript Fundamentals
Srdjan Strbanovic
 
Functional Programming in Swift
Saugat Gautam
 
When life gives you functions make functional programs!
Aaron Levin
 
Functional Programming in Javascript - IL Tech Talks week
yoavrubin
 
Functional Programming with JavaScript
WebF
 
Functional Programming in JavaScript by Luis Atencio
Luis Atencio
 
Declarative JavaScript concepts and implemetation
Om Shankar
 
Introduction to Functional Programming
Hoàng Lâm Huỳnh
 
Functionnal programming
AndryRajohnson
 
Functional programming
Nyarai Tinashe Gomiwa
 
Functional Programming
SovTech (Scrums.com)
 
Intro to functional programming - Confoo
felixtrepanier
 
Functional Programming Principles & Patterns
zupzup.org
 
Introduction to Functional programming
Ny Fanilo Andrianjafy, B.Eng.
 
Introduction to functional programming
Konrad Szydlo
 
Functional programming
Hideshi Ogoshi
 
Ad

More from Bruno Lui (6)

PDF
Switch
Bruno Lui
 
PDF
Refactoring
Bruno Lui
 
PPTX
Introdução ao Android
Bruno Lui
 
PDF
Inversion of control
Bruno Lui
 
PDF
Passionate programmer - Parte 1
Bruno Lui
 
PPTX
Clean Code
Bruno Lui
 
Switch
Bruno Lui
 
Refactoring
Bruno Lui
 
Introdução ao Android
Bruno Lui
 
Inversion of control
Bruno Lui
 
Passionate programmer - Parte 1
Bruno Lui
 
Clean Code
Bruno Lui
 
Ad

Recently uploaded (20)

PPTX
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PDF
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
PPTX
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
PDF
Is Framer the Future of AI Powered No-Code Development?
Isla Pandora
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PPTX
Library_Management_System_PPT111111.pptx
nmtnissancrm
 
PDF
Meet in the Middle: Solving the Low-Latency Challenge for Agentic AI
Alluxio, Inc.
 
PPTX
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
PPTX
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
PDF
Simplify React app login with asgardeo-sdk
vaibhav289687
 
PDF
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
PPTX
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
PDF
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
From spreadsheets and delays to real-time control
SatishKumar2651
 
Smart Doctor Appointment Booking option in odoo.pptx
AxisTechnolabs
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Top Agile Project Management Tools for Teams in 2025
Orangescrum
 
Get Started with Maestro: Agent, Robot, and Human in Action – Session 5 of 5
klpathrudu
 
Is Framer the Future of AI Powered No-Code Development?
Isla Pandora
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
Library_Management_System_PPT111111.pptx
nmtnissancrm
 
Meet in the Middle: Solving the Low-Latency Challenge for Agentic AI
Alluxio, Inc.
 
iaas vs paas vs saas :choosing your cloud strategy
CloudlayaTechnology
 
prodad heroglyph crack 2.0.214.2 Full Free Download
cracked shares
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Build a Custom Agent for Agentic Testing.pptx
klpathrudu
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
IObit Driver Booster Pro 12.4.0.585 Crack Free Download
henryc1122g
 
Simplify React app login with asgardeo-sdk
vaibhav289687
 
Download Canva Pro 2025 PC Crack Full Latest Version
bashirkhan333g
 
BB FlashBack Pro 5.61.0.4843 With Crack Free Download
cracked shares
 
Dipole Tech Innovations – Global IT Solutions for Business Growth
dipoletechi3
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
From spreadsheets and delays to real-time control
SatishKumar2651
 

Functional Programming

  • 3. modifying mutable variables using assignments and control structures such as if, then, else, lops, break, continue, return
  • 5. modifying mutable variables assignments programming without and other imperative control structures
  • 6. programming focusing on the functions functions ARE values that can be produced, consumed and composed
  • 8. Immutable Data Functional programs should be immutable, which means you would simply create new data structures instead of modifying ones that already exist.
  • 9. Referential transparency Functional programs should perform every task as if for the first time, with no knowledge of what may or may not have happened earlier in the program’s execution and without side effects.
  • 10. Function as first-class citizens functions can be defined anywhere they can be passed as parameters to functions and returned as results there exists a set of operators to compose functions
  • 11. • A number can be stored in a variable and so can a function: 
 var fortytwo = function() { return 42 }; • A number can be stored in an array slot and so can a function: 
 var fortytwos = [42, function() { return 42 }]; • A number can be stored in an object field and so can a function: 
 var fortytwos = {number: 42, fun: function() { return 42 }}; 
 • A number can be created as needed and so can a function: 
 42 + (function() { return 42 })(); //=> 84 • A number can be passed to a function and so can a function: 
 function weirdAdd(n, f) { return n + f() } weirdAdd(42, function() { return 42 }); 
 //=> 84 
 • A number can be returned from a function and so can a function: 
 return 42;
 return function() { return 42 }; 

  • 12. Composing Functions In functional programming, you’re always looking for simple, repeatable actions to be abstracted out into a function. We can then build more complex features by calling these functions in sequence
  • 13. "Functional programming is the use of functions that transform values into units of abstraction, subsequently used to build software systems."
  • 14. code examples Give a 10% discount to all products of the chart above $30 Show the total value of products and discount
  • 15. Imperative Double valorTotal = 0d; Double descontoTotal = 0d; for (Produto produto : produtos) { valorTotal += produto.getValor(); if (produto.getValor() > 30.00) { descontoTotal += produto.getValor() * 0.1; } } valorTotal -= descontoTotal;
  • 16. functional val valores = produtos map (_.valor) val valorTotal = valores reduce ((total, valor) => total + valor) val descontoTotal = valores filter (_ > 30.00) map (_ * 0.10) reduce ((total, desconto) => total + desconto) val total = valorTotal - descontoTotal
  • 17. code examples Write a program to build a lyric sheet for the song “99 bottles of beer" X bottles of beer on the wall X bottles of beer Take one down, pass it around X-1 bottles of beer on the wall No more bottles of beer on the wall
  • 18. Imperative var lyrics = []; 
 for (var bottles = 99; bottles > 0; bottles--) { lyrics.push(bottles + " bottles of beer on the wall”); lyrics.push(bottles + " bottles of beer"); lyrics.push("Take one down, pass it around"); if (bottles > 1) {
 lyrics.push((bottles - 1) + " bottles of beer on the wall."); } else { 
 lyrics.push("No more bottles of beer on the wall!”); } }
  • 19. functional function lyricSegment(n) { return _.chain([]) .push(n + " bottles of beer on the wall") .push(n + " bottles of beer”) .push("Take one down, pass it around") .tap(function(lyrics) { if (n > 1)
 lyrics.push((n - 1) + " bottles of beer on the wall."); else lyrics.push("No more bottles of beer on the wall!"); }) .value(); } 

  • 20. functional lyricSegment(9); //=> ["9 bottles of beer on the wall", // "9 bottles of beer", // "Take one down, pass it around", // "8 bottles of beer on the wall."]
  • 21. functional function song(start, end, lyricGen) { return _.reduce(_.range(start,end,-1), function(acc,n) {
 return acc.concat(lyricGen(n)); }, []); } And using it is as simple as: song(99, 0, lyricSegment); //=> ["99 bottles of beer on the wall", // ... // "No more bottles of beer on the wall!"]
  • 22. 1. All of your functions must accept at least one argument. 2. All of your functions must return data or another function. 3. No loops 3 simple rules to avoid imperative habits
  • 24. Coursera: Functional Programming Principles in Scala Book: Functional Javascript, Michael Fogus http://www.smashingmagazine.com/2014/07/02/dont-be-scared-of-functional- programming/ References