js authoritative guide _ chapter eight function _ functional programming sample code learning


    3 //Standard deviation is the most commonly used form of quantification to reflect the degree of dispersion of a set of data, and is an important indicator of accuracy. 
    //All numbers minus the sum of the squares of their averages, the result is divided by the number of the group number (or the number minus one, that is, the number of variance), and then the root sign of the obtained value is the number obtained The standard deviation. 
    var t=0;


    for(var i=0;i<data.length;i++){ 
        var deviation=data[i]-mean; 
        t+=deviation*deviation; 
    } 
    var stddev=Math.sqrt(t/(data.length-1)) ;//Standard deviation 
    console.log(stddev)//=>2 
} 

//fn1(); 

//Use array methods map() and reduce() to calculate the average and standard deviation of array elements 
function fn2(){ 
    var data=[1,1,3,5,5]; 
    var sum=function(x,y){return x+y}; 
    var square=function(x){return x*x}; 
    //reduce() use The specified function (called the simplification function) combines array elements and returns a single value. 
    var mean=data.reduce(sum)/data.length; 
    console.log(mean);//=>3 
    //map() traverse the array from beginning to end, call the specified function for each element, and specify the return of the function The values ​​(must) form a new array. 
    var deviationArr=data.map(function(x){return mean-x});
    var stddev=Math.sqrt(deviationArr.map(square).reduce(sum)/(data.length-1)); 
    console.log(stddev)//=>2 
}

//fn2(); 

//8.8.2 Advanced function 
function fn3(){ 
    //High-order function not() returns a new function odd, odd passes its actual parameter to f(even), odd returns f The logical negation of the return value of (even). 
    function not(f){ 
        return function(){//return a new function 
            //apply() allows to display the this value required for the specified call, that is, any function can be called as a method of any object, even if this function is not this Object method. 
            //apply() method requires parameters to be passed in in the form of an array. 
            var result = f.apply(this,arguments);//Call f() 
            return !result;//Invert the result 
        } 
    } 

    var even=function(x){//Judge whether it is an even number 
        return x%2 == = 0; 
    }; 
 
    var odd=not(even);//Judge whether it is odd
    var arr=[ 
    1,1,3,5,5 ]; console.log(arr.every(odd));//true 
} 

//fn3(); 

//Example 2 
//Custom Array map() method
var map=Array.prototype.map 
?function(a,f){return a.map(f)} 
:function(a,f){ 
    var results=[]; 
    for(var i= 0,len= a.length ;i<len;i++){ 
        //undo: I don’t understand why it is necessary to judge i in a 
        if(i in a){ 
            //call() method allows to display the value of this required for the specified call, that is, any function can Called as a method of any object, even if the function is not a method of this object. 
            //call() method uses its own argument list as the argument of the function 
            results[i]= f.call(null,a[i],i,a) 
        } 
    } 
    return results; 
}; 

function fn4() { 
    //mapper() generates a new function, the new function maps an array to another array that uses this function. 
    function mapper(f){
        return function(a){ 
            return map(a,f); 
        }; 
    } 
    var increment=function(x){return x+1;};
    var incrementer=mapper(increment); 

    console.log(incrementer([1,2,3]));//=>[2,3,4] 
} 

//fn4(); 

//8.8.3 incomplete function 
//Incomplete function is a function transformation technique, that is, a complete function call is split into multiple function calls, and the actual parameters passed in each time are part of the complete actual parameters, and each split function is called Incomplete function, each function call is called incomplete call. 

function fn5(){ 
    //Implement a tool function to convert an array-like object or object into a real array 
    function array(a,n){ 
        //slice() Returns a slice or sub-array of the specified array. With head and no tail, end can be negative. end omit truncation to the end. 
        return Array.prototype.slice.call(a,n || 0); 
    } 

    function partialLeft(f){ 
        var args=arguments; 
        return function(){ 
    } 
            var a=array(args,1);//Start processing external An args
            a = a.concat(array(arguments));//Then add all internal arguments 
            return f.apply(this,a);//Then call f() based on this argument list 
        } 

    var f=function(x ,y,z){return x*(yz);}; 

    console.log(partialLeft(f,2)(3,4));//=>2*(3-4)=-2 
} 

//fn5 (); 

//8.8.4 memory 
//memorize() receives a function as an actual parameter and returns a function with memory capabilities. 
function memorize(f){ 
    var cache={};//Save the value in the closure 
    return function(){ 
        //Convert the actual parameter to a string and use it as the key for the cache 
        //join() will All elements in the array are converted into strings and connected together according to the specified separator (comma by default), and the final generated string is returned. 
        var key=arguments.length + Array.prototype.join.call(arguments,","); 
        if(key in cache){ 
            return cache[key]; 
        }else{
            return cache[key] = f.apply(this,arguments) 
        } 
    }; 
} 
 
function fn6(){ 
    //Define a factorial function to cache the last calculation result.
    //Note: When we write a recursive function, we often need to implement the memory function. We prefer to call the recursive function that implements the memory function instead of the original recursive function. 
    var factorial=memorize(function(n){ 
        return (n <= 1)? 1: n * factorial(n-1); 
    }); 

    console.log(factorial(5));//=>5*4* 3*2*1=120 
} 

fn6(); 

</script> 
</body> 
</html>

Guess you like

Origin blog.csdn.net/Irene1991/article/details/105866140
Recommended