SlideShare a Scribd company logo
FUNCTIONAL
PROGRAMMING
Hideshi Ogoshi
YNS Philippines Inc.
Aug-11-2016
WHAT IS PARADIGM SHIFT
➤ Paradigm shift is a fundamental change in the basic concepts
and experimental practices of a scientific discipline.
Before Copernicus: The Sun goes around the Earth
After Copernicus: The Earth goes around the Sun
PARADIGM SHIFT IN PROGRAMMING LANGUAGE
From what to what?
From: Procedural programming
To: Functional programming
PROGRAMMING LANGUAGES MAP
C
Pascal
Fortran
BASIC
C++
Python
PHP
Java
C#
Ruby
JavaScript
Procedural
Object Oriented Functional
Scala
LISP
Haskell
OCaml
Clojure
Erlang
Elixir
DIFFERENCE BETWEEN PROCEDURAL AND FUNCTIONAL
➤Procedural programming language describes how to do.
array = [1, 2, 3, 4, 5];
result = [];
for(i = 0; i < length(array); i++) {
if(array[i] % 2 == 0) {
result.append(array[i] * 2);
}
}
➤Functional programming language describes what to do.
result = range(1 to 5)
.filter(fn(x){x % 2 == 0})
.map(fn(x){x * 2});
WHAT IS FUNCTION IN MATHEMATICS
➤ A function is a relation between a set of inputs and a set of
permissible outputs with the property that each input is
related to exactly one output.
f(x) = x + 2
f(1) = 1 + 2 = 3
f(3) = 3 + 2 = 5
f(3) = 3 + 2 = 3
WHAT IS FUNCTION IN PROCEDURAL PROGRAMMING LANGUAGE
➤ In the procedural programming
language, original meaning of
the function is exactly the same
as the definition of mathematics.
So it can return value.
fn plus_two(x) {
return x + 2;
}
res1 = plus_two(1);
print(res1);
==> 3
res2 = plus_two(3);
print(res1);
==> 5
➤ It can be used to process
something without returning value.
fn do_something(object) {
object.foo = ‘baz’;
}
obj = new SomeObject();
obj.foo = ‘bar’;
res = do_something(obj);
print(gettype(res));
==> NULL
print(obj);
==> {‘foo’ => ‘baz’}
WHAT IS FUNCTION IN FUNCTIONAL PROGRAMMING LANGUAGE
➤ In the functional
programming language,
meaning of the function is
exactly and strictly the same
as the definition of
mathematics. So it is always
expected to return value.
fn plus_two(x) {
x + 2;
}
res1 = plus_two(1);
print(res1);
==> 3
res2 = plus_two(3);
print(res1);
==> 5
TERMS OF FUNCTIONAL PROGRAMMING LANGUAGE
➤ First-class function
➤ Mutability and Immutability
➤ Referential transparency and Side effect
➤ Recursion
➤ Closure
➤ Lazy evaluation and Eager evaluation
FIRST-CLASS FUNCTION
➤ Functions are treated as first-class citizens in the language
➤ The language supports passing functions as arguments to
other functions, returning them as the values from other
functions, and assigning them to variables or storing them in
data structures.
➤ This kind of function is called higher-order function.
MUTABILITY AND IMMUTABILITY
➤ Mutability: Characteristic of able to be changed
foo = ‘bar’;
foo = ‘baz’;
➤ Immutability: Characteristic of unable to be changed
foo = ‘bar’;
foo = ‘baz’;
❌
REFERENTIAL TRANSPARENCY AND SIDE EFFECT
➤ Referential transparency is that a
function or an expression is said
to be referentially transparent if
it can be replaced with other
value without changing the
behavior of a program.
fn sum(array) {
result = 0;
i = 0;
len = length(array);
while(len > i) {
result += array[i];
i++;
}
return result;
}
fn sum(array) {
result = 0;
for(i = 0; i < length(array); i++) {
result += array[i];
}
return result;
}
➤ Side effect is that a function or
expression is said to have a side
effect if it modifies some state
or has an observable interaction
with calling functions or the
outside world.
fn do_many_thing_with_side_effect(obj) {
f = open(‘foo.txt’, ‘r’);
line = [];
while(line = f.readline()) {
lines.append(line);
print(line);
}
obj.data = lines;
}
RECURSION
➤ Recursion is one of the ways for functions to process a collection
data, such as list, by calling itself inside of itself.
➤ The function process the first element of the list and passing rest of
the list to itself. If the function consumed all the elements in the list,
the function just return accumulator as a result.
fn map(list, func, accumulator) {
if(length(list) == 0) {
return accumulator;
} else {
head, tail = list.split();
accumulator.append(func.call(head));
map(tail, func, accumulator);
}
}
res = map([1, 2, 3], plus2, []);
print(res);
==> [3, 4, 5];
map([1, 2, 3], plus2, []);
map([2, 3], plus2, [3]);
map([3], plus2, [3, 4]);
map([], plus2, [3, 4, 5]);
CLOSURE
➤ Closure is a kind of functions, which accepts variables or functions as arguments and makes
its behaviour dynamic in accordance with its arguments.
➤ Closure is supposed to return function.
fn make_plus(num) {
func = fn (x) {
return x + num;
}
return func;
}
plus1 = make_plus(1);
print(gettype(plus1));
==> function
print(plus1.call(2));
==> 3
plus3 = make_plus(3);
print(plus3.call(4));
==> 7
LAZY EVALUATION AND EAGER EVALUATION
➤ Lazy evaluation is one of the strategies to process the data by
postponing the evaluation of its arguments.
result = range(1 to 1_000_000_000)
.lazy_map(very_expensive_function)
.lazy_filter(fn(x) {
if(x % 2 == 0) {
return true;
} else {
return false;
})
.take(10);
MODULARITY OF FUNCTIONAL PROGRAMMING
➤ Modularity is one of the important ways to compose software for the maintainability, testability and
quality.
➤ It ensure each function to have its own responsibility and make those functions loosely coupling each other
Function without side effect
Function without side effect
Function without side effect
Function without side effect
Function with few side effects
Function with many side effects
BENEFIT OF FUNCTIONAL PROGRAMMING
➤ Realizes higher modularity, testability and quality by adopting
test driven development with peace of mind.
➤ Test driven development enable us to enhance continuous
integration.
➤ Make us easily understand languages or frameworks, which
are based on functional programming paradigm, such as
reactive functional programming.
FUNCTIONAL PROGRAMMING LANGUAGES
THANK YOU

More Related Content

What's hot (20)

PPT
Recursion
James Wong
 
PPTX
A brief introduction to lisp language
David Gu
 
PPT
INTRODUCTION TO LISP
Nilt1234
 
PPTX
LISP: Introduction to lisp
DataminingTools Inc
 
PPT
Function overloading(C++)
Ritika Sharma
 
PPTX
Optimization of dfa
Kiran Acharya
 
PDF
C# p5
Renas Rekany
 
PDF
C# p8
Renas Rekany
 
PPT
(Ai lisp)
Ravi Rao
 
PDF
AI Lesson 13
Assistant Professor
 
PPT
Hima1
Babul Miah
 
PPTX
Recursion
Ssankett Negi
 
ODP
Introduction to Programming in LISP
Knoldus Inc.
 
PDF
The Expression Problem - Part 1
Philip Schwarz
 
PPT
Cis068 08
FALLEE31188
 
PDF
AI Lesson 16
Assistant Professor
 
PDF
The Expression Problem - Part 2
Philip Schwarz
 
PDF
Introduction To Lisp
kyleburton
 
Recursion
James Wong
 
A brief introduction to lisp language
David Gu
 
INTRODUCTION TO LISP
Nilt1234
 
LISP: Introduction to lisp
DataminingTools Inc
 
Function overloading(C++)
Ritika Sharma
 
Optimization of dfa
Kiran Acharya
 
(Ai lisp)
Ravi Rao
 
AI Lesson 13
Assistant Professor
 
Hima1
Babul Miah
 
Recursion
Ssankett Negi
 
Introduction to Programming in LISP
Knoldus Inc.
 
The Expression Problem - Part 1
Philip Schwarz
 
Cis068 08
FALLEE31188
 
AI Lesson 16
Assistant Professor
 
The Expression Problem - Part 2
Philip Schwarz
 
Introduction To Lisp
kyleburton
 

Viewers also liked (20)

PDF
How to create test data
Hideshi Ogoshi
 
PPTX
Software Testing
Denmark Anthony Tan
 
PPTX
System performance tuning
Menandro Oba
 
PDF
Learning CakePHP2 from source code vol2
Hideshi Ogoshi
 
PPTX
Node js - Yns
Alex Amistad
 
PPTX
Php 7 - YNS
Alex Amistad
 
PPTX
Cake PHP 3 Presentaion
glslarmenta
 
PPTX
Falcon Full Text Search Engine
Hideshi Ogoshi
 
PPTX
Learning CakePHP from Source Code
Hideshi Ogoshi
 
PDF
The essence of Reactive Programming
Eddy Bertoluzzo
 
ODP
1.7 functional programming
futurespective
 
ODP
Elm & Elixir: Functional Programming and Web
Publitory
 
PDF
The taste of F#
☁️ Mikhail Shilkov
 
PDF
Designing with Capabilities
Scott Wlaschin
 
PDF
Doge-driven design
Scott Wlaschin
 
PDF
Real-World Functional Programming @ Incubaid
Nicolas Trangez
 
PDF
The Theory of Chains
Scott Wlaschin
 
PPTX
MySQL対応全文検索システムMroonga(むるんが)
Hideshi Ogoshi
 
PDF
Slick 3.0 functional programming and db side effects
Joost de Vries
 
How to create test data
Hideshi Ogoshi
 
Software Testing
Denmark Anthony Tan
 
System performance tuning
Menandro Oba
 
Learning CakePHP2 from source code vol2
Hideshi Ogoshi
 
Node js - Yns
Alex Amistad
 
Php 7 - YNS
Alex Amistad
 
Cake PHP 3 Presentaion
glslarmenta
 
Falcon Full Text Search Engine
Hideshi Ogoshi
 
Learning CakePHP from Source Code
Hideshi Ogoshi
 
The essence of Reactive Programming
Eddy Bertoluzzo
 
1.7 functional programming
futurespective
 
Elm & Elixir: Functional Programming and Web
Publitory
 
The taste of F#
☁️ Mikhail Shilkov
 
Designing with Capabilities
Scott Wlaschin
 
Doge-driven design
Scott Wlaschin
 
Real-World Functional Programming @ Incubaid
Nicolas Trangez
 
The Theory of Chains
Scott Wlaschin
 
MySQL対応全文検索システムMroonga(むるんが)
Hideshi Ogoshi
 
Slick 3.0 functional programming and db side effects
Joost de Vries
 
Ad

Similar to Functional programming (20)

ODP
Functional programming
S M Asaduzzaman
 
ODP
Introduction to R
agnonchik
 
PPTX
Intro f# functional_programming
Mauro Ghiani
 
PDF
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
KEY
Functional programming in clojure
Juan-Manuel Gimeno
 
PDF
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
PDF
Functional Programming in F#
Dmitri Nesteruk
 
PPT
Scala functions
Knoldus Inc.
 
PDF
Functional Programming by Examples using Haskell
goncharenko
 
PDF
Why you should care about functional programming
Dhananjay Nene
 
ODP
To Infinity & Beyond: Protocols & sequences in Node - Part 1
Bahul Neel Upadhyaya
 
PPT
Programming in Computational Biology
AtreyiB
 
PPTX
Data strutcure and annalysis topic stack
MihirMishra36
 
PPTX
Special topics in finance lecture 2
Dr. Muhammad Ali Tirmizi., Ph.D.
 
PPTX
Thinking Functionally with JavaScript
Luis Atencio
 
PPTX
Dev Concepts: Functional Programming
Svetlin Nakov
 
PDF
Composition birds-and-recursion
David Atchley
 
PPTX
Functional programming in javascript
Boris Burdiliak
 
PDF
Elements of Functional Programming in PHP
Jarek Jakubowski
 
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Functional programming
S M Asaduzzaman
 
Introduction to R
agnonchik
 
Intro f# functional_programming
Mauro Ghiani
 
TI1220 Lecture 6: First-class Functions
Eelco Visser
 
Functional programming in clojure
Juan-Manuel Gimeno
 
Functional Programming for OO Programmers (part 2)
Calvin Cheng
 
Functional Programming in F#
Dmitri Nesteruk
 
Scala functions
Knoldus Inc.
 
Functional Programming by Examples using Haskell
goncharenko
 
Why you should care about functional programming
Dhananjay Nene
 
To Infinity & Beyond: Protocols & sequences in Node - Part 1
Bahul Neel Upadhyaya
 
Programming in Computational Biology
AtreyiB
 
Data strutcure and annalysis topic stack
MihirMishra36
 
Special topics in finance lecture 2
Dr. Muhammad Ali Tirmizi., Ph.D.
 
Thinking Functionally with JavaScript
Luis Atencio
 
Dev Concepts: Functional Programming
Svetlin Nakov
 
Composition birds-and-recursion
David Atchley
 
Functional programming in javascript
Boris Burdiliak
 
Elements of Functional Programming in PHP
Jarek Jakubowski
 
Столпы функционального программирования для адептов ООП, Николай Мозговой
Sigma Software
 
Ad

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Designing Production-Ready AI Agents
Kunal Rai
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Designing Production-Ready AI Agents
Kunal Rai
 

Functional programming

  • 2. WHAT IS PARADIGM SHIFT ➤ Paradigm shift is a fundamental change in the basic concepts and experimental practices of a scientific discipline. Before Copernicus: The Sun goes around the Earth After Copernicus: The Earth goes around the Sun
  • 3. PARADIGM SHIFT IN PROGRAMMING LANGUAGE From what to what? From: Procedural programming To: Functional programming
  • 5. DIFFERENCE BETWEEN PROCEDURAL AND FUNCTIONAL ➤Procedural programming language describes how to do. array = [1, 2, 3, 4, 5]; result = []; for(i = 0; i < length(array); i++) { if(array[i] % 2 == 0) { result.append(array[i] * 2); } } ➤Functional programming language describes what to do. result = range(1 to 5) .filter(fn(x){x % 2 == 0}) .map(fn(x){x * 2});
  • 6. WHAT IS FUNCTION IN MATHEMATICS ➤ A function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output. f(x) = x + 2 f(1) = 1 + 2 = 3 f(3) = 3 + 2 = 5 f(3) = 3 + 2 = 3
  • 7. WHAT IS FUNCTION IN PROCEDURAL PROGRAMMING LANGUAGE ➤ In the procedural programming language, original meaning of the function is exactly the same as the definition of mathematics. So it can return value. fn plus_two(x) { return x + 2; } res1 = plus_two(1); print(res1); ==> 3 res2 = plus_two(3); print(res1); ==> 5 ➤ It can be used to process something without returning value. fn do_something(object) { object.foo = ‘baz’; } obj = new SomeObject(); obj.foo = ‘bar’; res = do_something(obj); print(gettype(res)); ==> NULL print(obj); ==> {‘foo’ => ‘baz’}
  • 8. WHAT IS FUNCTION IN FUNCTIONAL PROGRAMMING LANGUAGE ➤ In the functional programming language, meaning of the function is exactly and strictly the same as the definition of mathematics. So it is always expected to return value. fn plus_two(x) { x + 2; } res1 = plus_two(1); print(res1); ==> 3 res2 = plus_two(3); print(res1); ==> 5
  • 9. TERMS OF FUNCTIONAL PROGRAMMING LANGUAGE ➤ First-class function ➤ Mutability and Immutability ➤ Referential transparency and Side effect ➤ Recursion ➤ Closure ➤ Lazy evaluation and Eager evaluation
  • 10. FIRST-CLASS FUNCTION ➤ Functions are treated as first-class citizens in the language ➤ The language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures. ➤ This kind of function is called higher-order function.
  • 11. MUTABILITY AND IMMUTABILITY ➤ Mutability: Characteristic of able to be changed foo = ‘bar’; foo = ‘baz’; ➤ Immutability: Characteristic of unable to be changed foo = ‘bar’; foo = ‘baz’; ❌
  • 12. REFERENTIAL TRANSPARENCY AND SIDE EFFECT ➤ Referential transparency is that a function or an expression is said to be referentially transparent if it can be replaced with other value without changing the behavior of a program. fn sum(array) { result = 0; i = 0; len = length(array); while(len > i) { result += array[i]; i++; } return result; } fn sum(array) { result = 0; for(i = 0; i < length(array); i++) { result += array[i]; } return result; } ➤ Side effect is that a function or expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world. fn do_many_thing_with_side_effect(obj) { f = open(‘foo.txt’, ‘r’); line = []; while(line = f.readline()) { lines.append(line); print(line); } obj.data = lines; }
  • 13. RECURSION ➤ Recursion is one of the ways for functions to process a collection data, such as list, by calling itself inside of itself. ➤ The function process the first element of the list and passing rest of the list to itself. If the function consumed all the elements in the list, the function just return accumulator as a result. fn map(list, func, accumulator) { if(length(list) == 0) { return accumulator; } else { head, tail = list.split(); accumulator.append(func.call(head)); map(tail, func, accumulator); } } res = map([1, 2, 3], plus2, []); print(res); ==> [3, 4, 5]; map([1, 2, 3], plus2, []); map([2, 3], plus2, [3]); map([3], plus2, [3, 4]); map([], plus2, [3, 4, 5]);
  • 14. CLOSURE ➤ Closure is a kind of functions, which accepts variables or functions as arguments and makes its behaviour dynamic in accordance with its arguments. ➤ Closure is supposed to return function. fn make_plus(num) { func = fn (x) { return x + num; } return func; } plus1 = make_plus(1); print(gettype(plus1)); ==> function print(plus1.call(2)); ==> 3 plus3 = make_plus(3); print(plus3.call(4)); ==> 7
  • 15. LAZY EVALUATION AND EAGER EVALUATION ➤ Lazy evaluation is one of the strategies to process the data by postponing the evaluation of its arguments. result = range(1 to 1_000_000_000) .lazy_map(very_expensive_function) .lazy_filter(fn(x) { if(x % 2 == 0) { return true; } else { return false; }) .take(10);
  • 16. MODULARITY OF FUNCTIONAL PROGRAMMING ➤ Modularity is one of the important ways to compose software for the maintainability, testability and quality. ➤ It ensure each function to have its own responsibility and make those functions loosely coupling each other Function without side effect Function without side effect Function without side effect Function without side effect Function with few side effects Function with many side effects
  • 17. BENEFIT OF FUNCTIONAL PROGRAMMING ➤ Realizes higher modularity, testability and quality by adopting test driven development with peace of mind. ➤ Test driven development enable us to enhance continuous integration. ➤ Make us easily understand languages or frameworks, which are based on functional programming paradigm, such as reactive functional programming.