|
| 1 | +# Chapter 21: Higher-Order Functions And Functional Programming |
| 2 | + |
| 3 | +**`Higher Order Functions:`** |
| 4 | +A function which takes another function as an argument or returns a function from it, is know as a Higher order function. |
| 5 | + |
| 6 | +```javascript |
| 7 | +function x() { |
| 8 | + console.log("Hello"); |
| 9 | +} |
| 10 | + |
| 11 | +function y(x) { |
| 12 | + x(); |
| 13 | +} |
| 14 | +``` |
| 15 | + |
| 16 | +So, here `x` is a callback function and `y` is a higher order function. |
| 17 | + |
| 18 | +## Introduction to Functional Programming In JS: |
| 19 | + |
| 20 | +#### **NORMAL ZINDAGI CODE** |
| 21 | + |
| 22 | +```javascript |
| 23 | +const radiusOfCircles = [3, 1, 2, 4]; |
| 24 | + |
| 25 | +const calculateArea = function (radius) { |
| 26 | + const areas = []; |
| 27 | + |
| 28 | + for (let i = 0; i < radius.length; i++) { |
| 29 | + areas.push(Math.PI * radius[i] * radius[i]); |
| 30 | + } |
| 31 | + |
| 32 | + return areas; |
| 33 | +}; |
| 34 | + |
| 35 | +console.log(calculateArea(radiusOfCircles)); |
| 36 | + |
| 37 | +const calculatePerimeter = function (radius) { |
| 38 | + const perimeters = []; |
| 39 | + |
| 40 | + for (let i = 0; i < radius.length; i++) { |
| 41 | + perimeters.push(2 * Math.PI * radius[i]); |
| 42 | + } |
| 43 | + |
| 44 | + return perimeters; |
| 45 | +}; |
| 46 | + |
| 47 | +console.log(calculatePerimeter(radiusOfCircles)); |
| 48 | +``` |
| 49 | + |
| 50 | +This is what the most of the people normally try to do to. _[!! NORMAL ZINDAGI !!]_ |
| 51 | + |
| 52 | +What if the problem also stats to compute the diameter of the circle. So, one of the problem over here is that we are repeating ourselves. _[Avoids the principles of DRY(Do not Repeat Yourself)]_ |
| 53 | + |
| 54 | +#### **MENTOS ZINDAGI CODE** |
| 55 | + |
| 56 | +```javascript |
| 57 | +const radiusOfCircles = [3, 1, 2, 4]; |
| 58 | + |
| 59 | +const area = function (radius) { |
| 60 | + return Math.PI * radius * radius; |
| 61 | +}; |
| 62 | + |
| 63 | +const perimeter = function (radius) { |
| 64 | + return 2 * Math.PI * radius; |
| 65 | +}; |
| 66 | + |
| 67 | +const diameter = function (radius) { |
| 68 | + return 2 * radius; |
| 69 | +}; |
| 70 | + |
| 71 | +const calculateX = function (radii, fxnName) { |
| 72 | + const result = []; |
| 73 | + |
| 74 | + for (let i = 0; i < radii.length; i++) { |
| 75 | + result.push(fxnName(radii[i])); |
| 76 | + } |
| 77 | + |
| 78 | + return result; |
| 79 | +}; |
| 80 | + |
| 81 | +console.log(calculateX(radiusOfCircles, area)); |
| 82 | +console.log(calculateX(radiusOfCircles, perimeter)); |
| 83 | +console.log(calculateX(radiusOfCircles, diameter)); |
| 84 | +``` |
| 85 | + |
| 86 | +**Now why This code is good?** |
| 87 | +As we can clearly see that we have extracted our code in the smaller chunk, or we have made it modular in nature by introducing modularity. |
| 88 | + |
| 89 | +Each one of the function has one task to one task to perform the calculation. |
| 90 | + |
| 91 | +This is the advantage or the beauty of the functional programming. |
| 92 | + |
| 93 | +We have deduceds all the logic into smaller resuable logic which is function. |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +Here calculateX is almost similar to the map function of JS. |
| 98 | +So, somehow we have tried to mimic the same behaviour. |
| 99 | + |
| 100 | +```javascript |
| 101 | +const radiusOfCircles = [3, 1, 2, 4]; |
| 102 | + |
| 103 | +const area = function (radius) { |
| 104 | + return Math.PI * radius * radius; |
| 105 | +}; |
| 106 | + |
| 107 | +// This(Array.prototype) will make the function calculateX to be available to all our arrays. |
| 108 | +Array.prototype.calculateX = function (fxnName) { |
| 109 | + const result = []; |
| 110 | + |
| 111 | + // So here `this` points to the array on which it is being called upon. |
| 112 | + for (let i = 0; i < this.length; i++) { |
| 113 | + result.push(fxnName(this[i])); |
| 114 | + } |
| 115 | + |
| 116 | + return result; |
| 117 | +}; |
| 118 | + |
| 119 | +console.log(radiusOfCircles.calculateX(area)); |
| 120 | +``` |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +<br><br> |
| 125 | + |
| 126 | +<p align="left"> |
| 127 | + <a href="./20_setTimeout().md"><b>‹ GO TO PREVIOUS</b></a> |
| 128 | +</p> |
| 129 | + |
| 130 | +<p align="right"> |
| 131 | + <a href="./22_map_filter_reduce.md"><b>GO TO NEXT ›</b></a> |
| 132 | +</p> |
| 133 | + |
| 134 | +--- |
0 commit comments