FUNCTIONAL PROGRAMMING WHY FUNCTIONAL PROGRAMMING you will be
FUNCTIONAL PROGRAMMING
WHY FUNCTIONAL PROGRAMMING you will be able to write less code with less bugs and you will be able to reuse most of your code If we write functions that don’t have side-effects, then we can use them whenever and wherever we want to when composing our queries Concurrency is getting a real problem in normal programming because we're getting more and more multiprocessor computers. Testing is easy.
FIRST CLASS FUNCTION it means that the language treats functions as values – that you can assign a function into a variable, pass it around, returning them as the values from other functions, and assigning them to variables or storing them in data structures.
FUNCTION ARE VALUES function triple(x){ return x * 3 } we can create an anonymous function and we can assign that to variable like var tripal = function(x){ return x * 3 } triple(3); like strings or number we can pass function as variables
PURE FUNCTION A pure function is a function that, given the same input, will always return the same output and does not have any observable side effect. E. g. Splice and slice We say slice is pure because it returns the same output per input every time var sample. Arr = [1, 2, 3, 4, 5]; //pure sample. Arr. slice(0, 3); //=> [1, 2, 3] // impure sample. Arr. splice(0, 3); //=> [1, 2, 3] sample. Arr. splice(0, 3); //=> [4, 5] sample. Arr. splice(0, 3); //=> []
HIGHER ORDER FUNCTION higher order function allows us to create bigger function from smaller functions
MAP Map transforms the object into another array E. G. var kv. Array = [{key: 1, value: 10}, {key: 2, value: 20}, {key: 3, value: 30}]; var reformatted. Array = kv. Array. map(function(obj){ var r. Obj = {}; r. Obj[obj. key] = obj. value; return r. Obj; }); // reformatted. Array is now [{1: 10}, {2: 20}, {3: 30}], // kv. Array is still [{key: 1, value: 10}, {key: 2, value: 20}, {key: 3, value: 30}]
FILTER Filter applies some conditions on array and gives us the filtered array. function is. Big. Enough(value) { return value >= 10; } var filtered = [12, 5, 8, 130, 44]. filter(is. Big. Enough); // filtered is [12, 130, 44]
REDUCE The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value. var total = [0, 1, 2, 3]. reduce(function(a, b){ return a + b; }, 0); var flattened = [[0, 1], [2, 3], [4, 5]]. reduce(function(a, b) { return a. concat(b); }, []); // flattened is [0, 1, 2, 3, 4, 5]
COMPOSE Composing multiple functions to create more complex ones is a common utility in any programming language. And the ability to construct functions in a way that is easily composable is a true talent var add 1 = function(x) {return x + 1; }; var mult 2 = function(x) {return x * 2; }; var square = function(x) {return x * x; }; var negate = function(x) {return -x; }; console. log(add 1(42)); console. log(square(7)); var f = compose(add 1, square); console. log(f(7));
CHAINING Currying is an incredibly useful technique from functional Java. Script. It allows you to generate a library of small, easily configured functions that behave consistently, are quick to use, and that can be understood when reading your code. Adding currying to your coding practice will encourage the use of partially applied functions throughout your code, avoiding a lot of potential repetition, and may help get you into better habits about naming and dealing with function arguments. . function sum(a, b, c){ return a+b+c; }; console. log( my. Curry(sum)(1, 2, 3) ); // 6 console. log( my. Curry(sum)(1, 2)(3) ); //6 console. log( my. Curry(sum)(1)(2)(3) ); //6 function my. Curry(fn){ var arr. Args = []; return function x(){ for(var i = 0; i !== arguments. length; i ++ ){ arr. Args. push(arguments[i]); } if(arr. Args. length === 3){ return fn(. . . arr. Args); }else{ return x; } } }
PREFER COMPOSITION OVER INHERITANCE class Person{ constructor(fn, ln){ this. first. Name = fn; this. last. Name = ln; } get. Info(){ //In real life this method/function will be bigger return ‘Hi i am ‘ + this. first. Name + “ “ + this. last. Name; } } class Employee extends Person { constructor(fn, ln, eid){ super(fn, ln); this. empid = eid; } get. Id(){ return “Hi my employee id is “ + this. empid; } } const e 1 = new Employee(‘john’, ‘doe’, 123); console. log( e 1. get. Info() ); // Hi i am john doe console. log( e 1. get. Id() ); // Hi my employee id is 123 console. log(e 2. get. Id()); // Hi my employee id is 123 function get. Info(first. Name, last. Name) { return "Hi i am " + first. Name + " " + last. Name; } function create. Person(fn, ln) { return { get. Info: function () { return get. Info(fn, ln) } }; } function get. Id(empid) { return "Hi my employee id is " + empid; } function create. Employee(fn, ln, empid) { return { get. Id: function () { return get. Id(empid); }, get. Info: function () { return get. Info(fn, ln); } }; } var e 2 = create. Employee("john", "doe", 123); console. log(e 2. get. Info()); // Hi i am john doe console. log(e 2. get. Id()); // Hi my employee id is 123
QUESTIONS
- Slides: 13