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