Higher order Function: The art of minimalism.

Shavi went to Tanay with some coding doubts. But on her way, she overheard two amazing programmers talking about their project...

·

3 min read

When it came to programming and learning, shavi was a learner. A learner that delights the instructor. Her doubts were thought-provoking, while her projects were mind-boggling. But that day, the universe was planning something else. Something that may change everything. The way she codes, her designs, her thinking patterns, and above all her passion and interest. She heard them talking about the colors they used, and how they have used new tech with the old one. One said I have exactly 2000 lines of code, excluding the comments in my upcoming e-commerce project. But the other one had millions in different files altogether. This is the exact copy of all the big ecoms you have ever seen, he exclaimed with pride in his eyes. *"My Best program till now has 500 lines max, including the comments." Said Tanay! That was the magic of functional programming.

Great things often lie in small changes.

  • Small programs are most likely to be correct. Methods like abstraction, and inheritance do not change the logic but just with little rearrangement and presentation techniques, we can make the code much more readable and simple.

Take half a cup of water, if it is just for one, and put it into an utensil suitable for heating. crush an inch of ginger and let it boil with the water, same way add 2 cardamoms or its powder into the refreshing boiling mixture. Let this boil for 2 minutes in medium flame. Add tea leaves which is measures as one teaspoon per person. After adding mix it and let it boil for 3 minutes with the mixture now add one spoon of sugar and wait until it is mixed. End this tea with a half cup of milk. Let this get 2-3 boils for 5 to 10 minutes. Now, carefully take this out in a cup and serve with 2-3 biscuit or paratha.

*per person: half cup of milk, half cup of water, 1teaspoon tea leaves, 1/2 spoon of sugar, 1-inch ginger, and 2 cardamoms.

logic: Let the measured water boil for 5 minutes with ginger and cardamom. Add tea leaves and sugar followed by milk and let it boil for 10 more minutes. *

As humans, we tend to get more mistakes in the first logic rather than the second one though the functions are the same. In this blog, we will explore the need for functional programming.

The Regular Abstraction and Higher-order function.

Sometimes, the old plain function we use is not enough. For example, whenever we see a repeat, as programmers we use a loop.

for(int i = 0; i<10; i++){
     console.log("I am  winner", i );
}

Can we abstract this? Well, that's easy. Wrap this with a function.

function repeat(n){
       for(int i = 0; i<10; i++){
     console.log("I am  winner" , i );
      }
}

But, What if we want more? Something else? Functions are nothing more than values so we can pass them as another function value.

function repeat(n, action) {
  for (let i = 0; i < n; i++) {
    action( "I am a winner" , i);
  }
}

repeat(3, console.log);
// →  I am a winner 0
// →  I am a winner 1
// →  I am a winner 2

Often, its easier to create function value on the spot.

let labels = [];
repeat(5, i => {
  labels.push(` Winner ${i + 1}`);
});
console.log(labels);
//output->[
0:    " Winner 1"
1:    " Winner 2"
2:    " Winner 3"
3:    " Winner 4"
4:    " Winner 5"
]

Higher Order Functions

Functions that operate on other functions are HOF(Higher order functions). They allow us to abstract actions, not just values. There are many more concepts in realated and if you feel uncomfortable, Congratulations you are going to get there. Till then, lets read and practice more! Shavi.