"JS Authoritative Guide Learning summary--8.8.3 incomplete function"

Source: Internet
Author: User
Tags pow

Content Highlights:

This section discusses a function transformation technique that splits a complete function call into multiple function calls, each of which is part of a complete argument, each split function called an incomplete function (partial function), and each function call is called an incomplete call (partial application), this function transformation is characterized by the return of a function for each invocation until the final run result is obtained, and a simple example is to modify the call to function f (1,2,3,4,5,6) to equivalent F (three) (5,6), which consists of a, The function associated with each invocation is the "incomplete function".

One.

1. The bind () method of function f () returns a new function that passes in a specific context and a specified set of parameters to the new function, and then calls F ().

We say it binds the function to the object and passes in some parameters. The bind () method simply places the argument to the left of the (full argument list), meaning that the arguments passed in to bind () are placed at the beginning of the argument list that passed in to the original function, but sometimes we expect to place the arguments of the incoming bind () on the right side of the (Full argument list):

Implement a tool function to convert a class array object (or object) to a true array

This method is used in the following example code to convert a arguments object to a true array

function Array (a,n) {return Array.prototype.slice.call (a,n| | 0); }

The argument of this function is passed to the left

function Partialleft (f/*,... */) {

var args = arguments; Save the external real parameter group

return function () {//and returns this

var a= array (args,1); Start processing the outer 1th args

A = A.concat (array (arguments)); Then add all the internal arguments

Return f.apply (This,a); Then call F () based on this argument list

};

}

The argument of this function is passed to the right

function Partialright (f/*,... */) {

var args = arguments; Saving external Real parameter groups

return function () {

var a = array (arguments); Start with the internal parameters

A = A.concat (Array (args,1)); and add it from the outside 1th args.

Return f.apply (This,a); Finally call F () based on this argument list

};

}

The argument of this function is used as a template

Undefined values in the argument list are populated

function partial (f/*,... */) {

var args = arguments; Saving external Real parameter groups

return function () {

var a = array (args,1); Start with the external args

var i = 0,j = 0;

Traversing args, populating undefined values from internal arguments

for (; i<a.length;i++)

if (a[i] = = = undefined) a[i] = arguments[j++];

Now append the remaining internal arguments to the

A = A.concat (Array (arguments,j))

Return f.apply (This,a);

};

}

This function is with three arguments

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

Note the difference between these three incomplete calls

Partialleft (f,2) (3,4)//=>-2: Binding first argument: 3-4

Partialright (f,2) (3,4)//=>6: Binding last argument: 4-2

Partial (f,undefined,2) (3,4)//=>-6: Binding Intermediate Arguments: 2-4

Two. Use existing functions to define new functions

With this incomplete function programming technique, you can write interesting code that uses existing functions to define new functions:

var increment = Partialleft (sum,1);

var cuberoot = partialright (MATH.POW,1/3);

String.prototype.first = partial (string.prototype.charat,0);

String.prototype.last = partial (string.prototype.substr,-1,1);

When you combine incomplete calls with other higher-order functions, things become particularly interesting. For example, the example here defines the not () function, which uses the incomplete invocation just mentioned:

var not = Partialleft (compose,function (x) {return!x;});

var even = function (x) {return x%2 ==0;};

var odd = not (even);

var isnumber = not (IsNaN);

We can also use a combination of incomplete calls to reorganize the code for averages and standard deviations, a programming style that is purely functional programming:

var data = [1,1,3,5,5]; The data we have to deal with

var sum = function (x, y) {return x+y;}; Two elementary functions

var produce = function (x, y) {return x*y;};

var neg = partial (product,-1);

var square = partial (math.pow,undefined,2);

var sqrt = partial (math.pow,undefined,. 5);

var reciprocal = partial (math.pow,undefined,-1);

Now calculate mean and standard deviation, all function calls without operators

var mean =product (reduce (data,sum), reciprocal (data.length));

var stddev = sqrt (product (Reduce (map (data,compose (squaer,partial (Sum, neg (mean))), sum), reciprocal (SUM ( data.length,-1)));

"JS Authoritative Guide Learning summary--8.8.3 incomplete function"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.