This presentation is about the Functional Programming Paradigm. The differences between the Imperative Paradigm, a few principles and concepts and some code examples.
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
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();
}
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