Enumerable#Zip 实现一下

早上看到“geff Zhang”介绍了Enumerable#Zip,闲来没事弄一个实现。

也谈不上思路,看了张兄的测试代码,先写了个简单的结构:

代码语言:javascript
复制
public static IEnumerable<TResult> Zip<TFirst, TSencond, TResult>(
            this IEnumerable<TFirst> first, 
            IEnumerable<TSencond> sencond, 
            Func<TFirst, TSencond, TResult> func)
        {
    }</code></pre></div></div><p>有了结构,实现就简单多了。张兄在文中介绍到“方法将第一个序列中的每个元素与第二个序列中有相同索引的元素合并。如果该序列不具有相同数目的元素,则直到它到达其中一个的末尾,该方法才合并序列。例如,如果一个序列有三个元素,另一个序列具有四个元素,那么结果序列将只有三个元素。”</p><p>要读到两个序列的各个元素,按照相同索引的元素,执行func,原先想想用foreach,for之类的,好像有点麻烦,为了简单,就直接转换成IEnumerator。</p><p>上代码,大家看了就明白了:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">public static IEnumerable&lt;TResult&gt; Zip&lt;TFirst, TSencond, TResult&gt;(
        this IEnumerable&lt;TFirst&gt; first, 
        IEnumerable&lt;TSencond&gt; sencond, 
        Func&lt;TFirst, TSencond, TResult&gt; func)
    {
        var firsttor = first.GetEnumerator();
        var sencondtor = sencond.GetEnumerator();

        while (firsttor.MoveNext())
        {
            if (sencondtor.MoveNext())
            {
                yield return func(firsttor.Current, sencondtor.Current);
            }
        }
    }</code></pre></div></div><p>随便写的,也没在意性能方面,等大家一起讨论下好了。</p><p>附上测试代码:</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0">int[] numberic = new int[] { 1, 2, 3, 4 };
        string[] words = new string[] { &#34;a&#34;, &#34;b&#34;, &#34;c&#34;, &#34;d&#34; };
        int[] numbericDiff = new int[] { 1, 2, 3 };

        var result = numberic.Zip(words, (a, b) =&gt; a + &#34; &#34; + b);

        foreach (var r in result)
        {
            Console.WriteLine(r);
        }

        result = numbericDiff.Zip(words, (a, b) =&gt; a + &#34; &#34; + b);

        foreach (var r in result)
        {
            Console.WriteLine(r);
        }</code></pre></div></div><p>结果:</p><figure class=""><div class="rno-markdown-img-url" style="text-align:center"><div class="rno-markdown-img-url-inner" style="width:29.34%"><div style="width:100%"><img src="https://cdn.static.attains.cn/app/developer-bbs/upload/1723168002142256159.png" /></div><div class="figure-desc">image</div></div></div></figure><p>PS:推一个旅游网站,大家工作之余也要放松自己嘛。</p><p>http://www.sh-bus.com</p>