locked
IEnumerable Zip RRS feed

  • Question

  • Hi I am struggling to understand why public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> first, IEnumerable<TSecond> second, Func<TFirst, TSecond, TResult> resultSelector); ss called Zip and not Merge ? Best Regards Yogesh
    Thursday, February 17, 2011 10:45 AM

Answers

  • The reason it is called "Zip" instead of "Merge" is that Zip works on pairs of elements with the same source index (like doing up a zip, where pairs of teeth are engaged as you do it up), whereas a merge outputs elements that may be drawn from the same source collection.

    Also normally a merge will output elements that are the same type as the input collections, whereas a zip may output elements that are of a different type.

    For example, imagine you have two collections of strings as follows:

    A F
    B H
    C J
    E K
    G P
    I
    L

    Merging those two sequences in alphabetical order would result in:

    A
    B
    C
    E
    F
    G
    H
    I
    J
    K
    L
    P

    Zipping the two sequences with a concatenation operator would result in:

    "A F"
    "B H"
    "C J"
    "E K"
    "G P"
    "I"
    "L"

    • Marked as answer by Yogesh Pawar Tuesday, February 22, 2011 12:17 PM
    Monday, February 21, 2011 9:55 AM

All replies

  • Zip acts on elements and creates 1 element out of two.
    Thursday, February 17, 2011 11:01 AM
  • Hi Yogesh Pawar,

    What is your concern, just the method name or others?

    This method merges two sequences and concatenate two sequences into one.

    If you have any concern, please feel free to descript your concern more clarifier.

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, February 18, 2011 2:47 AM
  • Hi,

    Any update?

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Saturday, February 19, 2011 4:35 PM
  • Hi Larcolais

     

    Its not very clear from the name what it would be doing.

    So far I understand it will create 1 element from two in two seqs e.g. {1,2,3} Zip { 1,2,3} => { 2,4,6}(provided action was to add)

    Thats what you said merge elements of 2 seqs

    When you say concatenate what exactly do you mean?

     

    Best Regards

    Yogesh

    Monday, February 21, 2011 9:42 AM
  • The reason it is called "Zip" instead of "Merge" is that Zip works on pairs of elements with the same source index (like doing up a zip, where pairs of teeth are engaged as you do it up), whereas a merge outputs elements that may be drawn from the same source collection.

    Also normally a merge will output elements that are the same type as the input collections, whereas a zip may output elements that are of a different type.

    For example, imagine you have two collections of strings as follows:

    A F
    B H
    C J
    E K
    G P
    I
    L

    Merging those two sequences in alphabetical order would result in:

    A
    B
    C
    E
    F
    G
    H
    I
    J
    K
    L
    P

    Zipping the two sequences with a concatenation operator would result in:

    "A F"
    "B H"
    "C J"
    "E K"
    "G P"
    "I"
    "L"

    • Marked as answer by Yogesh Pawar Tuesday, February 22, 2011 12:17 PM
    Monday, February 21, 2011 9:55 AM