What is the Enumerable Zip in . NET?

Asked

Viewed 138 times

7

I was making a code to combine two lists, and looking to improve discovered the method Zip of the interface IEnumerable.

At first I thought it was some algorithm to "zip" a list/array/etc., but I saw that it does a combination of the elements of two enumerable lists.

As it did not serve what I needed I went to look for another solution, but it seemed interesting, and I would like to know more about, and maybe help other people here.

Example of the documentation link:

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.Zip(words, (first, second) => first + " " + second);

Hence the doubt, what exactly does the .Zip?

Some example of a practical utility?

  • Good morning Ricardo, beauty? A good explanation with examples found on this site here. Take a look: https://docs.microsoft.com/pt-br/dotnet/api/system.linq.enumerable.zip?view=netframework-4.8 valeuu!!

  • @That’s exactly the link that’s in my question :)

2 answers

10


The confusion with compaction comes from the fact that some compression mechanisms use the term to say they squeeze something, which is still a function of zipper since it adjusts something loose to be fairer, smaller. But the action itself is just take two parts and go synchronizing until it becomes one thing, doesn’t mean it gets smaller in fact.

What is a zipper?

It’s a mechanism that joins two parts together making it look like one.

Zipper de roupa

What it actually does is just take the die on one side and join with the die on the other side in some specified way, just as it happens on one side zipper, each gummy on one side fits with the gummy on the other side, as shown by the example of the question.

Creativity can be great in what to do with the data coming from the two sequences of enumerable data, what matters is just to have these sequences and an algorithm that says what result to generate, is a form of pairing.

Examples

Could take two values and mount tuples or data types together.

You could marry states that are on one side and capitals that are on the other and create an object that contains the two information. It could be the complete list of existing drives or it could be a list of individual data from something that makes sense, but which are separated into different objects.

It has a certain type of Nosql database that the data of the same entity gets separated, it is a misuse of technology, but it could build the entity with a Zip() which gathers the data received separately.

You can only create an object with a sequential shape, so on one side you will have an enumerator generated at sequential time from 1 to the total size of items, on the other side with the data of the other sequence, only for the objects to have an explicit numbering, who knows it would be a id.

You can create total values by taking on one side the quantity and on the other the unit value, just by making a simple multiplication.

The enumeration can come from anywhere, it can be up to a function that picks up a field of the items of a collection on one side and another field of the same object of the collection. It is very common to use compound with other methods such as LINQ.

You can create a JSON where you join the descriptive names of the JSON members with the data coming from somewhere else, something like that:

using static System.Console;
using System.Linq;

public class Program {
    public static void Main() {
        var header = new string[2] { "nome", "endereco" };
        var joao = new string[2] { "joão", "rua santa cruz, 28" };
        var json = header.Zip(joao, (header, data) => "\"" + header + "\" : \"" + data + "\"");
        foreach (var item in json) WriteLine(item);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

I didn’t put together the whole JSON, because for that I wouldn’t have to do a array two-dimensional, or else have a function in the collection that creates the collection of data from the object that would be likely in real code to produce the list of fields, only to pass the idea of use.

Of course the combinations should be simple, if you have to make a complex algorithm starts to lose its validity, you can’t find information that matches well with other, think about gumdrops.

Helps to understand: Javascript . NET Zip() method.

See the source of the method how simple.

  • 2

    "It’s a mechanism that joins two parts together making her stay as if she were one" Wow, I hadn’t thought of that, it makes perfect sense! ;)

6

Basically it allows you to combine two lists, through a function you write according to your need.

That post has a legal example, where it combines the list of state acronyms with a list of state names. Thinking about the Brazilian scenario, we could have a ZIP code list and match the street names.

Below follows the code snippet.

string[] codes = { "AL", "AK", "AZ", "AR", "CA" };
string[] states = 
{
   "Alabama", 
   "Alaska", 
   "Arizona", 
   "Arkansas", 
   "California" 
};

var CodesWithStates = 
   codes.Zip(
      states, 
      (code, state) => code + ": " + state);

foreach ( var item in CodesWithStates )
    Console.WriteLine(item);

The exit will be:

AL: Alabama
AK: Alaska
AZ: Arizona
AR: Arkansas
CA: California

  • 1

    Rafael, transcribe a piece of the code of that link in your answers to not only stay the link, that one day can stay off the air and lose the sense of the answer ;)

  • show @Ricardopontual, thanks for the tip!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.