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

PPT
MYSQL Aggregate Functions
PPT
PHP mysql Aggregate functions
PPTX
Aggregate Function - Database
PPT
Aggregate functions
PPTX
Aggregate function
PDF
Lec21-CS110 Computational Engineering
PDF
Web app development_php_06
PPTX
C Programming Language Step by Step Part 2
MYSQL Aggregate Functions
PHP mysql Aggregate functions
Aggregate Function - Database
Aggregate functions
Aggregate function
Lec21-CS110 Computational Engineering
Web app development_php_06
C Programming Language Step by Step Part 2

What's hot (19)

PPTX
C Programming Language Part 7
PPSX
C programming function
PPTX
Lessons learned from functional programming
PPSX
Functions in c
PPTX
Call by value
PPTX
Call by value or call by reference in C++
PPTX
parameter passing in c#
PPTX
Functions in C
PPT
Functions in C++
PPTX
Function (rule in programming)
PPT
Pre defined Functions in C
PPTX
functions of C++
PPTX
C Programming Language Part 6
PPTX
C++ lecture 03
PPTX
How c program execute in c program
PPTX
C function presentation
PPT
16717 functions in C++
 
PPT
Functions in C++
PPT
Single row functions
C Programming Language Part 7
C programming function
Lessons learned from functional programming
Functions in c
Call by value
Call by value or call by reference in C++
parameter passing in c#
Functions in C
Functions in C++
Function (rule in programming)
Pre defined Functions in C
functions of C++
C Programming Language Part 6
C++ lecture 03
How c program execute in c program
C function presentation
16717 functions in C++
 
Functions in C++
Single row functions
Ad

Similar to Functional Programming (20)

PPTX
Functional programming in JavaScript
PPTX
Functions
PPTX
CHAPTER 01 FUNCTION in python class 12th.pptx
PDF
Python_Functions.pdf
PDF
Programming Fundamentals Functions in C and types
PPT
functions _
PDF
PDF
The Ring programming language version 1.9 book - Part 28 of 210
PDF
The Ring programming language version 1.5.1 book - Part 20 of 180
PDF
Chapter 1. Functions in C++.pdf
PDF
Chapter_1.__Functions_in_C++[1].pdf
PPTX
Fundamentals of functions in C program.pptx
PDF
The Ring programming language version 1.8 book - Part 26 of 202
PDF
VIT351 Software Development VI Unit1
PPTX
Functional Programming in JavaScript by Luis Atencio
PPT
Functions in c++
DOCX
PDF
4th unit full
PPTX
Function in c program
Functional programming in JavaScript
Functions
CHAPTER 01 FUNCTION in python class 12th.pptx
Python_Functions.pdf
Programming Fundamentals Functions in C and types
functions _
The Ring programming language version 1.9 book - Part 28 of 210
The Ring programming language version 1.5.1 book - Part 20 of 180
Chapter 1. Functions in C++.pdf
Chapter_1.__Functions_in_C++[1].pdf
Fundamentals of functions in C program.pptx
The Ring programming language version 1.8 book - Part 26 of 202
VIT351 Software Development VI Unit1
Functional Programming in JavaScript by Luis Atencio
Functions in c++
4th unit full
Function in c program
Ad

More from Bruno Lui (6)

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

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
history of c programming in notes for students .pptx
PDF
Cost to Outsource Software Development in 2025
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Digital Strategies for Manufacturing Companies
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Nekopoi APK 2025 free lastest update
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
medical staffing services at VALiNTRY
System and Network Administraation Chapter 3
Digital Systems & Binary Numbers (comprehensive )
history of c programming in notes for students .pptx
Cost to Outsource Software Development in 2025
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Operating system designcfffgfgggggggvggggggggg
Odoo POS Development Services by CandidRoot Solutions
PTS Company Brochure 2025 (1).pdf.......
Digital Strategies for Manufacturing Companies
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Nekopoi APK 2025 free lastest update
Reimagine Home Health with the Power of Agentic AI​
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
medical staffing services at VALiNTRY

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