Blog Day 25: Why Higher-Order Function?

Inseo Park·2021년 8월 1일
0

Path to Data Scientist

목록 보기
24/58
post-thumbnail

1. TIL (Today I Learned)

Why higher-order function?

Abstraction

Abstraction is the of obtaining or removing something from a source.

We have no way to express what -1 is in reality but we call agree that -1 is 1 lesser than 0. This hows that if we use abstraction, we can think easily and efficiently.

Examples of abstraction in our life
1. When you type a web address, do you know what happens on the inside?
2. When you send a message to your friend via Instagram?
3. A engine start button in your car.

Javascript and other programming languages are all results of abstracting. The CPU (Central-Processing Unit) only understands 0s and 1s.
When you use the chrome developer's console to process the codes below, you don't know how it happnes, because the Javascript engine does it for you.

	function sum(num1, num2) {
    		return num1 + num2
            }
    	const output = sum (3, 7);
        console.log(output); // --> 10
        

Abstraction = increased productivity!

We also put logics that are used several times into a function, which is a good example of abstraction. In the abstraction point of view, function is a mixture and group of logics and thoughts.

The getAverage function below returns the average of a number type parameter. From now on we don't have to worry about the logic & just user the getAverage function to get a mean of a number.

	function getAverage(data) {
    		let sum = 0;
            for (let i = 0; i < data.length; i++) {
            sum = sum + data[i];
            }
            return sum / data.length;
            }
    
    	let output = getAverage ([1, 2, 3]);
        console.log(output); // --> 2
        
        output = getAverage([4, 2, 3, 6, 5, 4]);
        console.log(output); // --> 4
        

A higher-order function is abstraction occured on the next level of the abstraction we just saw. getAverage function uses the array to perform complicated tasks.

value-level abstraction = receives a value to perfrom complicated tasks
logic-level abstraction = receives a function to perform complicated tasks

Using the higher-order function will lead to a even higher productivity.

	 const data = [
          {
            gender: 'male',
            age: 24,
          },
          {
            gender: 'male',
            age: 25,
          },
          {
            gender: 'female',
            age: 27,
          },
          {
            gender: 'female',
            age: 22,
          },
          {
            gender: 'male',
            age: 29,
          },
        ];
        
        function getAverageAgeOfMaleAtOnce(data) {
        const onlyMales = data.filter(function (d) {
        // data.filter receives the value of the data set
        // only returns if it matches the argument
        return d.gender === 'male'
       });
       
       const numOfMales = onlyMales.length;
       
       const onlyMaleAges = onlyMales.map(function(d) {
       // onlyMaleAges.reduce receives the values of a dataset and performs the argument and puts it into the accumulative variable
       return acc + cur;
       }, 0);
       
       return sumOfAges / numOfMales;
       }
       

This is a not-bad function but if you change the 'male' part as another variable 'gender' it would be used to find both the average of male & female.

	function getOnlyMales(data) {
    	return data.filter(function (d) {
        return d.gender === 'male';
        });
        }
        
        function getOnlyAges(data) {
        return data.map(function (d) {
        return d.age;
        });
        }
        
        function getAverage(data) {
        const sum = data.reduce(function(acc,cur) {
        return acc + cur;
        }, 0);
        return sum / data.length;
        }
        
        function compose (...funcArgs) {
        // compose is a higher-order function that receives several functions and returns a function that recevies a data set.
        return function(data) {
        let result = data;
        for(let i = 0; i < funcArgs.length; i++) {
        result = funcArgs[i](result);
        } 
        return result;
        };
        }
        
        const getAverageAgeOfMale = compose(
        getOnlyMales,
        getOnlyAges,
        getAverage
        );
        
        
        const result = getAverageAgeOfMale(data);
        console.log(result); // --> 26
        

2. 3 Things to be Thankful for

  1. Thankful to come through a good recovery and becoming humble
  2. Thankful for having a good day to study and a another rest day tomorrow.
  3. Thankful for the rain and a not so hot weather.

3. Ideas and Things to Think about

  1. I should not overthink, no one is so perfect they don't make mistakes.
  2. Make mistakes and learn, people will judge you but be strong.
profile
my journey to become a data scientist.

0개의 댓글