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.