In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Unlike Lisp and Haskell, javascript is not a functional programming language, but it can manipulate the same functions as objects in javascript, that is to say, functional programming technology can be applied in javascript. Array methods in ES5, such as map () and reduce (), are well suited for functional programming styles. This article will introduce functional programming in detail.
Function processing array
Suppose you have an array whose elements are numbers, and you want to calculate the average and standard deviation of these elements. If you use a non-functional programming style, it is as follows
Var data = [1, var, 1, 3, 5, 5]; var total = 0 for var (I = 0; I)
< data.length; i++){ total += data[i];}var mean = total/data.length;total = 0;for(var i = 0; i < data.length; i++){ var deviation = data[i] - mean; total += deviation * deviation;}var stddev = Math.sqrt(total/(data.length-1)); 可以使用数组方法map()和reduce()实现同样的计算,这种实现极其简洁 var sum = function(x,y){ return x+y;}var square = function(x){ return x*x;}var data = [1,1,3,5,5];var mean = data.reduce(sum)/data.length;var deviations = data.map(function(x){ return x - mean;});var stddev = Math.sqrt(deviations.map(square).reduce(sum)/(data.length-1)); 在ES3中,并不包含这些数组方法,需要自定义map()和reduce()函数 //对于每个数组元素调用函数f(),并返回一个结果数组//如果Array.prototype.map定义了的话,就使用这个方法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++){ if(i in a){ results[i] = f.call(null,a[i],i,a); } } return results; } //使用函数f()和可选的初始值将数组a减到一个值//如果Array.prototype.reduce存在的话,就使用这个方法var reduce = Array.prototype.reduce ? function(a,f,initial){ if(arguments.length >2) {return a.reduce (fMagna initial);} else {return a.reduce (f);}}: function (aMagic f initial) {var I = 0, len = a.length, accumulator If (argument.length > 2) {accumulator = initial;} else {if (len = = 0) {throw TypeError () } while (I < len) {if (i in a) {accumulator = a [iTunes +]; break;} else {ionization + }} if (I = = len) {throw TypeError () }} while (I < len) {if (i in a) {accumulator = f.call (undefined,accumulator,a [I], iMagna);} return accumulator }
Higher order function
A higher order function (higher-order function) is a function that operates a function. It takes one or more functions as arguments and returns a new function.
/ / this higher-order function returns a new function that passes its argument into f () and returns the logical non-function not of the return value of f (f) {return function () {var result = f.apply (this,arguments); return! result;};} var even = function (x) {return x% 2 = 0;} var odd = not (even);
The not () function above is a higher-order function because it takes a function as an argument and returns a new function
The following mapper () function also takes a function as an argument and returns a new function that maps an array to another array that uses this function
/ / the parameters of the returned function should be a real argument group, and the function f () should be executed for each array element, and an array of all the calculated results function mapper (f) {return function (a) {return map (a);}} var increment = function (x) {return xylene 1;} var incrementer = mapper (increment); increment ([1Jing 2Jing 3]); / / [2LJ 3]
Here is a more common example that takes two functions f () and g () and returns a new function to calculate f (g ())
/ / return a new f (g (...)) that can be calculated The function h () returned by / / passes all its arguments into g (), and then passes the return value of g () into f () / / when calling f () and g (), and when it is called h () is the same thisfunction compose (fMagne g) {return function () {/ / needs to pass a parameter to f (). So using the call () method of f () / / needs to pass a lot of parameters to g (), so use the apply () method return f.call (this,g.apply (this,arguments) of g ()) };} var square = function (x) {return x;} var sum = function (x) {return x + y;} var squareofsum = compose (square,sum); squareofsum (2Magne3); / / 25
Incomplete function
Incomplete function is a kind of function transformation technique, that is, a complete function call is divided into multiple function calls, each incoming argument is part of a complete argument, and each split function is called an incomplete function. each function call is called an incomplete call. The characteristic of this function transformation is that a function is returned for each call until the final run result is obtained.
The bind () method of the function f () returns a new function, passes in a specific context and a specified set of parameters to the new function, and then calls the function f (). The bind () method just puts the arguments to the left of the complete argument list, that is, the arguments passed into bind () are placed at the beginning of the argument list of the original function, but sometimes you want to put the arguments passed in bind () to the right of the full argument list.
/ / implement a utility function to convert a class array object (or object) into a real array function array (array n) {return Array.prototype.slice.call (an arguments n | | 0);} / / the argument of this function is passed to the left function partialLeft (f) {var args = arguments; return function () {var a = array (args,1); a = a.concat (array (arguments)); return f.apply (this,a) };} / / the argument to this function is passed to the right function partialRight (f) {var args = arguments; return function () {var a = array (arguments); a = a.concat (array (args,1)); return f.apply (this,a);};} / / the argument to this function is used as a template, and the undefined values in the argument list are populated with function partial (f) {var args = arguments Return function () {var a = array (args,1); var I = 0, j = 0; / / traverses args, filling the undefined value for (; I) from the internal argument
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.