Yield a practical and concise way of implementing in JavaScript

Source: Internet
Author: User
Tags compact continue throw exception

Just now suddenly brainwave, iterators we rarely really directly to the silly next to traverse, then why must realize this stupid next? Directly implement each, so that, in turn, yeah, Itong, soon wrote the first Super Compact version:

Code

function Yieldhost (yieldfunction)
{
return function (Processer)
{
var yield = function (Result)
{
Processer (Result)
};

Yieldfunction (yield);

};
}

The idea of a change, the code is really concise.

Attach an example first and then talk about the principle.

First we need a function to enumerate, like this:

function Fun (yield)
{

for (var i = 0; i < i++)
Yield (i);

}

or so:

function Fun (yield)
{

Yield (1);
Yield (2);
Yield (3);

}

Because the implementation is different from C #, there is no yield break or yield continue in the loop, either directly or continue.

And then the actual application, the Yieldhost function converts the above-conforming fun function to an enumerator:

var enumerator = Yieldhost (fun);

This enumerator is actually a function, like each function of jquery, that receives a handler function to handle the enumeration:

Enumerator (function (item)
{
Window.alert (item);
});

Let's talk about the principle next.

For a traditional enumerator, we think that the enumerator should return a value at each call, which is the next method, but as Chen Zixian says, this requires stopping the function in yield, although it can be done, but it's really troublesome.

But! In fact, I've found that most of the time we use the syntax of a foreach to access the enumerator. This gives me a very flattering way to implement each method instead of implementing the next method.

What is the difference between each method and Next method? Friends who are familiar with jquery will know that each method can actually be considered to turn next, not to return an enumeration value, but rather to receive a function that takes an enumeration value as a parameter.

It was this fall that all the problems were solved. There is no need to suspend the execution of a function, just take a logical note of the enumeration value into this function and it's over. So in fact, the yieldhost here is to do a flip job, take the function that enumerator received (i.e. Window.alert (item), and note it in the enumeration function (that is, fun). The effect of the final execution is like this:

function Fun (yield)
{

Window.alert (1);
Window.alert (2);
Window.alert (3);

}

So it was born with this super simple realization.

With this super compact implementation, the next step is to achieve the same as the jquery each method of return true to represent the break and return false to represent the function of continue, only with such a function to handle the infinite set, Or to implement functions such as TakeWhile.

To be honest, my research on JavaScript is not thorough, and I only think of a way to use abnormal interrupts, which is the second version of Yieldhost:

Code

function Yieldhost (yieldfunction)
{

var exception = Math.random ();

return function (Processer)
{
Try
{
Yieldfunction (function (Result)
{
if (processer (Result))
Throw exception;
});
}
catch (E)
{
if (e!== exception)
Throw e;
}

};
}

Obviously it's not perfect, but I can't think of a better way.

The next step on this basis is to implement the Select, where what, in fact, is a very simple thing, give a realization of my select:

Code

function Select (enumerator, selector)
{
return function (Fun)
{
Enumerator (function (item)
{
Return Fun (Selector (item));
});
}

As for, this select how to modify into ligatures version, namely:

Enumerator. Select (selector) (processor);

I think this is really not a very difficult thing for JavaScript ....

It's just that the early introduction of grammatical friendliness can make javascript very complicated and unsightly. So, let's leave it to everyone to play.

Related Article

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.