C #4.0 new features (4) New LINQ Extension Method-zip ()

Source: Internet
Author: User
Tags zip extension

C #4.0 new features (4) New LINQ Extension Method-zip ()

1. Introduction

The so-called zip (Chinese indicates a zipper) is to stitch two lists together like a zipper. There is a zip function in Python that can be used to conveniently merge two or more sets, for example:

>>> Firstname = ['freesc ', 'jobua', 'ken']
>>> Lastname = ['huang ', 'guany', 'wang']
>>> For F, L in zip (firstname, lastname ):
Print ('{0} {1}'. Format (F, L ))

The above code is printed

Freesc Huang
Joshua Guan
Ken Wang

In C #4.0, we can see a similar extension function [1]:

Code 2

//
// Summary:
// Merges two sequences by using the specified predicate function.
//
// Parameters:
// First:
// The first sequence to merge.
//
// Second:
// The second sequence to merge.
//
// Resultselector:
// A function that specifies how to merge the elements from the two sequences.
//
// Type parameters:
// Tfirst:
// The type of the elements of the first input sequence.
//
// Tsecond:
// The type of the elements of the second input sequence.
//
// Tresult:
// The type of the elements of the result sequence.
//
// Returns:
// An system. Collections. Generic. ienumerable <t> that contains merged elements
// Of two input sequences.
Public static ienumerable <tresult> zip <tfirst, tsecond, tresult> (this ienumerable <tfirst> first,
Ienumerable <tsecond> second,
Func <tfirst, tsecond, tresult> resultselector );

It can be used to merge lists and provides custom combination rules: func <tfirst, tsecond, tresult> resultselector.

2. Example

The following is a C #4.0 program with the same function as code 1:

Code 3

List <string> firstname = new list <string> {"freesc", "Joshua", "Ken "};
List <string> lastname = new list <string> {"Huang", "Guan", "Wang "};
Foreach (VAR name in firstname. Zip (lastname, (fname, lname) => fname + "" + lname ))
{
Console. writeline (name );
}

 

3. Implementation of zip ()

To implement a zip file in Python, it is very simple (Exception Processing is omitted here). You only need to use three built-in functions, ITER, map, and next:

Def zip (* iterables ):
# Zip ('abcd', 'xy') --> ax
Iterables = map (ITER, iterables)
While iterables:
Yield tuple (MAP (next, iterables ))

 

Similarly, if you do not consider exception handling, the C # zip extension method can be implemented as follows [2]:

Code 5

Static class enumerable
{
Public static ienumerable <tresult> zip <tfirst, tsecond, tresult> (this ienumerable <tfirst> first,
Ienumerable <tsecond> second,
Func <tfirst, tsecond, tresult> func)
{
VaR ie1 = first. getenumerator ();
VaR ie2 = second. getenumerator ();

While (ie1.movenext () & ie2.movenext ())
Yield return func (ie1.current, ie2.current );
}
}

4. Summary

As a new member of the LINQ system, zip provides a way to freely combine two sets. Note that the length of the two sequences must be consistent during the use of zip. If they are different, it has a short length of yield. This is the same as zip in Python. In addition, you may try to write a multizip method that combines multiple sets, which may be more useful to you ;-)

5. Reference

Http://msdn.microsoft.com/en-us/library/dd267698 (vs.100). aspx

[2] http://community.bartdesmet.net/blogs/bart/archive/2008/11/03/c-4-0-feature-focus-part-3-intermezzo-linq-s-new-zip-operator.aspx

 

Author: freesc Huang @ cnblogs

Related Article

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.