Functional Programming within JavaScript
Kherson,
IT Trends,
October 20, 2016
goo.gl/za4BL1
Eugene Safronov
evgeniy.safronov@outlook.com
skype: lambda.omega1
I love JavaScript, i write on JavaScript, I learn JavaScript and I teach JavaScript
About me
Programming paradigm
OOP
Imperative
Declarative
Functional programming
Logic programming
procedure-oriented programming
Program within OOP
All programming model is based on the mathematical foundations
λ-calculus is a mathematical foundation of FP
Founding fathers
Haskell Brooks Curry
Alonzo Church
Mathematical function is the central concept of the functional paradigm
f(x) = x+sin(x)
Declarative vs Imperative
JavaScript is a multi-paradigm programming language
event-driven
object-oriented programming
procedure-oriented programming
functional programming
JavaScript platform
Elm
ClojureScript
Scala.js
compiled
JS
FP features
"No" cycles
"No" branch
"No" variables
"You can not" modify objects
"No" side effects in functions
Key concepts
Immutable
Pure functions
Higher-order functions
Lambda functions
Partial application
Carrying
Function composition
Imperative
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;
}
}
Declarative
VS
Immutable object cannot be modified after it is created.
Pure function is a function which:
- Given the same input, will always return the same output.
- Produces no side effects.
- Relies on no external state.
In programming a side effect is when a function changes a variable from outside its scope
Function behaviour
arguments
return value
side causes
side effects
Higher-order function: take functions as args or/and return function as result
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 };
}
Examples:
f(x,y) => x +y
f(2, y) => f(y) => 2 + y
Partial application is process of fixing a number of args to a function
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 ]
Example:
Currying is the technique of translating the evaluation of function that takes multiple args into evaluating a sequence of functions, each with a single argument
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
Example:
Function composition is pointwise application of one function to the result of another to produce a third function
(g °f)(x) = g(f(x))
function compose(func1, func2) {
return function() {
return func1(func2.apply(null, arguments));
};
}
Example:
Advantages
Caching
Parallelism
Testing
Dynamic language ( code - is also data)
Automatic optimization
Declarative
Disadvantage
Not easy
Difficult to optimize
Sometimes we need to use side effect
Libs & Tools
Immutable.js https://facebook.github.io/immutable-js/
Lodash https://lodash.com
Ramda.js http://ramdajs.com/
Bacon.js https://baconjs.github.io/
?
Thank you!
Functional Programming within JavaScript
By Evgeniy Safronov
Functional Programming within JavaScript
- 1,636