使用yield可以做哪些很酷的事情?

WBOY
Freigeben: 2016-06-06 16:23:18
Original
1614 Leute haben es durchsucht

使用生成器(Generator)和yield可以做哪些有趣的、酷酷的、让人意想不到的事情?
不限编程语言,例如python、JavaScript 等。

回复内容:

yield 在 JavaScript 中用的最多的可能就是结合 Promise/Thunk 等实现异步操作,比如大名鼎鼎的 tj/co · GitHub,所以已经不是「让人意想不到」的东西了。
理解 Generator 的特性后,实现一个玩具版的 co 还是很简单的:
<code class="language-text">function async(generator) {
  return new Promise(function(resolve, reject) {
    var g = generator()

    function next(val) {
      var result = g.next(val)
      var value = result.value

      if (!result.done) {
        value.then(next).catch(reject)
      }
      else {
        resolve(value)
      }
    }

    next()
  })
}
</code>
Nach dem Login kopieren
最典型的不就是async/await么?


不了解yield怎么实现async/await的,用C#代码试举一例:

<code class="language-text">IEnumerable<action>> SomeAsyncMethod()
{
  //blabla
  yield return await( asyncMethod, context );

  //blabla
  yield return await( asyncMethod, context );

  //blabla
}
</action></code>
Nach dem Login kopieren
可以做动画呀,效果如图:
<code class="language-python"><span class="c"># -*- coding: utf-8 -*-</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">matplotlib.animation</span> <span class="kn">as</span> <span class="nn">animation</span>
<span class="kn">import</span> <span class="nn">math</span><span class="o">,</span> <span class="nn">random</span>
<span class="c"># 需要安装的库:Numpy和Matplotlib,推荐直接Anaconda</span>
<span class="n">fig</span><span class="p">,</span> <span class="n">axes1</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">subplots</span><span class="p">()</span>
<span class="c"># 设置坐标轴长度</span>
<span class="n">axes1</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.4</span><span class="p">)</span>
<span class="n">axes1</span><span class="o">.</span><span class="n">set_xlim</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">/</span><span class="mf">0.01</span><span class="p">)</span>
<span class="c"># 设置初始x、y数值数组</span>
<span class="n">xdata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
<span class="n">ydata</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">xdata</span><span class="p">)</span>
<span class="c"># 获得线条</span>
<span class="n">line</span><span class="p">,</span> <span class="o">=</span> <span class="n">axes1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">xdata</span><span class="p">)</span>
<span class="c"># 毛刺倍率,从0开始增长,offset越大毛刺越大</span>
<span class="n">offset</span> <span class="o">=</span> <span class="mf">0.0</span>

<span class="c">#因为update的参数是调用函数data_gen,所以第一个默认参数不能是framenum</span>
<span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
    <span class="k">global</span> <span class="n">offset</span>
    <span class="n">line</span><span class="o">.</span><span class="n">set_ydata</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">line</span><span class="p">,</span>
<span class="c"># 每次生成10个随机数据</span>
<span class="c"># 每次变化整幅图的话,yield一个整图就行了</span>
<span class="k">def</span> <span class="nf">data_gen</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">offset</span>
    <span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
        <span class="n">length</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xdata</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xdata</span><span class="p">)):</span>
            <span class="n">ydata</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">=</span><span class="n">math</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">xdata</span><span class="p">[</span><span class="n">i</span><span class="p">])</span><span class="o">+</span><span class="mf">0.2</span>
            <span class="k">if</span> <span class="n">i</span><span class="o">></span><span class="n">length</span><span class="o">/</span><span class="mf">18.0</span> <span class="ow">and</span> <span class="n">i</span><span class="o"><span class="p">(</span><span class="n">length</span><span class="o">*</span><span class="mf">2.7</span><span class="o">/</span><span class="mf">6.0</span><span class="p">):</span>
                <span class="n">ydata</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">+=</span><span class="n">offset</span><span class="o">*</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">()</span><span class="o">-</span><span class="mf">0.5</span><span class="p">)</span>
        <span class="n">offset</span> <span class="o">+=</span> <span class="mf">0.05</span>
        <span class="c">#可以设置offset的最大值</span>
        <span class="k">if</span> <span class="n">offset</span><span class="o">>=</span><span class="mf">0.5</span><span class="p">:</span>
           <span class="n">offset</span><span class="o">=</span><span class="mf">0.0</span>
        <span class="k">yield</span> <span class="n">ydata</span>
<span class="c"># 配置完毕,开始播放</span>
<span class="n">ani</span> <span class="o">=</span> <span class="n">animation</span><span class="o">.</span><span class="n">FuncAnimation</span><span class="p">(</span><span class="n">fig</span><span class="p">,</span> <span class="n">update</span><span class="p">,</span> <span class="n">data_gen</span><span class="p">,</span> <span class="n">interval</span><span class="o">=</span><span class="mi">800</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</span></code>
Nach dem Login kopieren
模拟离散事件,还有更简洁优雅的方式么

Overview — SimPy 3.0.8 documentation 这个问题就是给我准备的嘛

当有人声称在CPython里实现了一个沙盒的时候就可以用yield去逗他了,I was looking through the code and saw someone submitted this but didn't run it:...

酷到没工作... A Curious Course on Coroutines and Concurrency 可以写出一个并发的库
Generator Tricks for Systems Programmers 可以写个流处理框架 参见David Beazley大神几次PyCon的pdf,看完我简直是惊呆了。dabeaz.com 可以用来训练神经网络.
比如Lasagne/Lasagne · GitHub 中的一段示例代码:
<code class="language-python"><span class="k">def</span> <span class="nf">train</span><span class="p">(</span><span class="n">iter_funcs</span><span class="p">,</span> <span class="n">dataset</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="n">BATCH_SIZE</span><span class="p">):</span>
    <span class="sd">"""Train the model with `dataset` with mini-batch training. Each</span>
<span class="sd">       mini-batch has `batch_size` recordings.</span>
<span class="sd">    """</span>
    <span class="n">num_batches_train</span> <span class="o">=</span> <span class="n">dataset</span><span class="p">[</span><span class="s">'num_examples_train'</span><span class="p">]</span> <span class="o">//</span> <span class="n">batch_size</span>
    <span class="n">num_batches_valid</span> <span class="o">=</span> <span class="n">dataset</span><span class="p">[</span><span class="s">'num_examples_valid'</span><span class="p">]</span> <span class="o">//</span> <span class="n">batch_size</span>

    <span class="k">for</span> <span class="n">epoch</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">batch_train_losses</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_batches_train</span><span class="p">):</span>
            <span class="n">batch_train_loss</span> <span class="o">=</span> <span class="n">iter_funcs</span><span class="p">[</span><span class="s">'train'</span><span class="p">](</span><span class="n">b</span><span class="p">)</span>
            <span class="n">batch_train_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_train_loss</span><span class="p">)</span>

        <span class="n">avg_train_loss</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_train_losses</span><span class="p">)</span>

        <span class="n">batch_valid_losses</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="n">batch_valid_accuracies</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_batches_valid</span><span class="p">):</span>
            <span class="n">batch_valid_loss</span><span class="p">,</span> <span class="n">batch_valid_accuracy</span> <span class="o">=</span> <span class="n">iter_funcs</span><span class="p">[</span><span class="s">'valid'</span><span class="p">](</span><span class="n">b</span><span class="p">)</span>
            <span class="n">batch_valid_losses</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_valid_loss</span><span class="p">)</span>
            <span class="n">batch_valid_accuracies</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">batch_valid_accuracy</span><span class="p">)</span>

        <span class="n">avg_valid_loss</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_valid_losses</span><span class="p">)</span>
        <span class="n">avg_valid_accuracy</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">batch_valid_accuracies</span><span class="p">)</span>

        <span class="k">yield</span> <span class="p">{</span>
            <span class="s">'number'</span><span class="p">:</span> <span class="n">epoch</span><span class="p">,</span>
            <span class="s">'train_loss'</span><span class="p">:</span> <span class="n">avg_train_loss</span><span class="p">,</span>
            <span class="s">'valid_loss'</span><span class="p">:</span> <span class="n">avg_valid_loss</span><span class="p">,</span>
            <span class="s">'valid_accuracy'</span><span class="p">:</span> <span class="n">avg_valid_accuracy</span><span class="p">,</span>
        <span class="p">}</span>
</code>
Nach dem Login kopieren
tornado就是使用generator实现的协程(coroutine)模型,再配合event loop实现高并发的 使用迭代器遍历二叉树。
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage