Skip to content

luketevl/functional-programming

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

functional-programming

Learn about functional programming

Material

Pure Functions

  • 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;
}

Hight Order Functions

  • The functions that:
    • They operate on other functions
    • Received as a parameters
    • Return functions
    let calculate = function(fn, x, y){
      return fn(x, y);
    }
    

Map

  • 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]

Filter

  • 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]

Reduce

  • 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

Currying

  • 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

  • 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!

About

Learn about functional programming

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published