Kherson,
IT Trends,
October 20, 2016
goo.gl/za4BL1
Eugene Safronov
evgeniy.safronov@outlook.com
skype: lambda.omega1
OOP
Imperative
Declarative
Functional programming
Logic programming
procedure-oriented programming
Haskell Brooks Curry
Alonzo Church
event-driven
object-oriented programming
procedure-oriented programming
functional programming
compiled
Immutable
Pure functions
Higher-order functions
Lambda functions
Partial application
Carrying
Function composition
const numbers = [1, 2, 3, 4, 5];
numbers
.map(n => n * 2)
.filter(n => n > 3)
.reduce((a, b) => a + b);
var numbers = [1, 2, 3, 4, 5], sum = 0, num;
for(var i = 0; i < numbers.length; i++){
num = numbers[i];
num *= 2;
if(num > 3){
sum += num;
}
}
Function behaviour
arguments
return value
side causes
side effects
const flattened = [[0, 1], [2, 3], [4, 5]].reduce((a, b) => a.concat(b), []);
// flattened is [0, 1, 2, 3, 4, 5]
const isBigEnough = (element, index, array) => element >= 10;
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
function magic(x) {
return function calc(x) { return x * 42 };
}
f(x,y) => x +y
f(2, y) => f(y) => 2 + y
const mul = (x, y) => x * y;
const doubleValue = mul.bind(null, 2);
doubleValue(6); //12
//[1, 2, 3, 4].map(e => mul(2, e));
[1, 2, 3, 4].map(doubleValue); //[ 2, 4, 6, 8 ]
f(x,y,z) => x + y + z
f(x,y,z) = curry(x)(y)(z)
const perimeterOfTriangle = (a, b, c) => a + b + c;
const curried = _.curry(perimeterOfTriangle);
perimeterOfTriangle(3, 4, 5);
curried(3)(4)(5); //12
curried(3)(_, 5)(4); //12
function compose(func1, func2) {
return function() {
return func1(func2.apply(null, arguments));
};
}
Not easy
Difficult to optimize
Sometimes we need to use side effect
Immutable.js https://facebook.github.io/immutable-js/
Lodash https://lodash.com
Ramda.js http://ramdajs.com/
Bacon.js https://baconjs.github.io/