Enumerable # zip implementation

Source: Internet
Author: User

In the morning, I saw "geff Zhang" Introducing enumerable # zip, so I had nothing to do with it.

Let's not talk about the idea. I read brother Zhang's test.CodeFirst, write a simple structure:

  Public static   ienumerable   
   
     zip 
    
      (
    
     This   ienumerable   
   
     first, 
     ienumerable   
   
     sencond, 
     func   
   
     func) {}
    

With the structure, the implementation is much simpler. In this article, Dr. Zhang introduced that "the method merges each element in the first sequence with the element with the same index in the second sequence. If the sequence does not have the same number of elements, the method merges the sequence until it reaches the end of one of them. For example, if a sequence has three elements and the other sequence has four elements, the result sequence has only three elements ."

To read each element of the two sequences and execute func based on the elements of the same index, it seems a little troublesome to use foreach, for, and so on. For simplicity, it is directly converted into ienumerator.

After reading the code, you will understand:

 Public static  Ienumerable  <Tresult> zip <tfirst, tsencond, tresult> (  This  Ienumerable  <Tfirst> first,  Ienumerable <Tsencond> sencond,  Func  <Tfirst, tsencond, tresult> func ){  VaR  Firsttor = first. getenumerator ();  VaR  Sencondtor = sencond. getenumerator ();  While  (Firsttor. movenext ()){  If (Sencondtor. movenext ()){  Yield return  Func (firsttor. Current, sencondtor. Current );}}} 

I did not care about performance when I wrote it casually. I will discuss it with you.

Attached test code:

 Int  [] Numberic =  New int  [] {  1  ,  2 ,  3  ,  4  };  String  [] Words =  New String  [] {  ""  ,  "B"  , "C"  ,  "D"  };  Int  [] Numbericdiff =  New int  [] {  1  ,  2  ,  3 };  VaR  Result = numberic. Zip (words, (a, B) => A +  ""  + B );  Foreach  (  VaR  R  In  Result ){  Console . Writeline (r);} result = numbericdiff. Zip (words, (a, B) => A +  ""  + B );  Foreach  (  VaR  R  In  Result ){  Console  . Writeline (r );} 

Result:

 

PS: when pushing a travel website, you have to relax yourself after work.

Http://www.sh-bus.com

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.