Generator functions and functions Thunk

Thunk

Thunk to call by name, that argument into a temporary function, the function passed to the function and then this temporary body.

function (m){
return m * 2;
}

f(x + 5);


var thunk = function () {
return x + 5;
};
function main(thunk){
return thunk() * 2;
}

x is a parameter, which put a temporary function, with really only when necessary to call, passing the main function, the typical functional programming ideas.

In JS, Thunk function replacement is not an expression, but multi-parameter function, it is replaced by a single parameter version, and only accept the callback function as an argument , in essence, currying function .

function Thunk(fn){
return function () { // save the parameters of the function closure var args = the Array .prototype.slice.call ( arguments ); // returns a function parameter is only the callback return function ( the callback ) { // increase the callback function parameter list args.push ( the callback); return fn.apply ( the this , args); } }; };










Thunk just not much use, it requires a combination Generator for process management.

Generator

Generator function returns an iterator Iterator objects, we can use this iterator traverses manually correlation value, the state, to ensure the correct order of execution. Generator function with a next method.

By next function, to yield the corresponding expression may be performed, and the next () function can also take parameter, which can be used as the expression returns a yield value, yield because there is no return value itself, if the next () does not parameters, the yield of each operation are in the return value is undefined

function* gen() {
// ...
}

where g = gene ();
was res = g.next ();

while(!res.done){
console.log(res.value);
res = g.next();
}
//执行next会返回{value: "one", done: false}

Generator letter big column  Thunk function and Generator function number simply returns a pointer to the object inside the iteration, under normal circumstances, we need to take the initiative to trigger the next () is executed, so with only Generator is not suitable for asynchronous operation.

yieldThe key generator function execution is suspended, yieldthe value of the expression after the keyword returned to the caller to the generator. It can be considered a generator-based version of returnthe keyword.
yieldKeywords actual return a IteratorResultobject that has two properties, valueand done. valueProperty is yieldthe result of expression evaluation, and doneis falseshowing a generator function has not been fully completed.

If the generator is passed to the optional value next()method, the generator current value becomes yieldthe value returned by the operation

Thunk automatic process management functions

Thunk function of real power, that can automatically execute Generator function

FUNCTION  run ( fn ) { were gene = fn ();


function Next ( ERR, Data ) { var ; Result = gen.next (Data) / * pointer to move to the next step Generator function * / IF (result.done) return ; / * is determined whether the end * / result.value ( next); / * recursive, the next into the .value in * / // after thunk function parameters only callback, here is the next function! ! , }






next();
}

was g = function * (){
var f1 = yield readFile('fileA');
var f2 = yield readFile('fileB');
// ...
var fn = yield readFile('fileN');
};

run(g);

first next function pointer to a function Generator The next step (gen.next method), and then determines whether to end the function Generator (result.done attributes), if not finished, the next function reinjecting Thunk function (result.value Properties ), otherwise exit


Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11691158.html