Learn about functional programming
- https://www.quora.com/What-is-a-side-effect-in-programming
- https://egghead.io/lessons/javascript-redux-pure-and-impure-functions
- https://wiki.haskell.org/Pointfree
- https://medium.com/@cscalfani/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536#.g1itz0qga
- Have inputs and outputs well defined
function square(x){
return x * x;
}
- Don't have side-effects
- Whenever your entry is 5 return will be the same.
func(3); // Result WHENEVER is 4
function func(x) {
return x + 1;
}
- The functions that:
- They operate on other functions
- Received as a parameters
- Return functions
let calculate = function(fn, x, y){ return fn(x, y); }
- The function invoke a callback and return one new array with result, apply item by item
const numbers = [1, 2, 3];
const square = x => x * x;
const squaredNumbers = numbers.map(square); // [1, 4, 9]
- The function invoke a callback and return one new array with result, return the filter the elements in initial array with base in function calback.
const numbers = [1, 4, 7, 10];
const isBiggerThanFour = value => value > 4 ;
const numbersBiggerThanFour = numbers.filter(isBiggerThanFour); // [7, 10]
- The function received with parameters callback functions and a initial value, transform the array in unique value
const numbers = [1, 2, 3];
const sum = (x, y) => x + y ;
const numbersSum = numbers.reduce(sum, 0); //6
- Transform the function with multiples parameters in the sequency the functions that have one parameter
const greeting = greet => name => `${greet} ${name}`;
const hello = greeting('Hello');
hello('World'); // Hello World
- Compose small functions and create complex functions, more reuse
const compose = (f, g) => x => f(g(x));
const toUpperCase = x => x.toUpperCase();
const exclaim = x => x + '!';
const angry = compose(toUpperCase, exclaim);
angry('ahhh'); // AHHH!