发电机功能中的授权产量(产量星,产量*) [英] Delegated yield (yield star, yield *) in generator functions

查看:109
本文介绍了发电机功能中的授权产量(产量星,产量*)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ECMAScript 6应该带来生成函数和迭代器。一个生成函数(它具有函数* 语法)返回一个迭代器。迭代器有一个下一个方法,当被重复调用时,它执行生成器函数的正文,每次的收益率重复暂停并恢复执行。 c $ c> operator。



ECMAScript 6维基发生器还引入了委托收益率 yield * 运算符如下:


yield * 运算符委托给另一个生成器。这提供了组合发电机的便利机制。


委托给另一个生成器是什么意思?如何使用 yield * 方便地撰写生成器?



[可以在Node v0中播放生成器.11.3与 - 和声发生器标志。]

解决方案

代替另一个生成器意味着当前的生成器自身停止生成值,而是产生另一个生成器生成的值,直到它耗尽它。



例如,如果 secondGenerator()生成来自 10 15 firstGenerator() code> 1 5 ,而是在生成 secondGenerator() code> 2 ,则由 firstGenerator()生成的值将是:

  1,2,10,11,12,13,14,15,3,4,5 


ECMAScript 6 should be bringing generator functions and iterators. A generator function (which has the function* syntax) returns an iterator. The iterator has a next method which, when repeatedly called, executes the body of the generator function, repeatedly pausing and resuming execution at every yield operator.

The ECMAScript 6 wiki on generators also introduces the "delegated yield" yield* operator as follows:

The yield* operator delegates to another generator. This provides a convenient mechanism for composing generators.

What does "delegate to another generator" mean? How can I use yield* to "conveniently compose generators"?

[You can play with generators in Node v0.11.3 with the --harmony-generators flag.]

解决方案

Delegating to another generator means the current generator stops producing values by itself, instead yielding the values produced by another generator until it exhausts it. It then resumes producing its own values, if any.

For instance, if secondGenerator() produces numbers from 10 to 15, and firstGenerator() produces numbers from 1 to 5 but delegates to secondGenerator() after producing 2, then the values produced by firstGenerator() will be:

1, 2, 10, 11, 12, 13, 14, 15, 3, 4, 5

这篇关于发电机功能中的授权产量(产量星,产量*)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆