miércoles, 13 de julio de 2016

LINQ Recipe No. 2-17: Collections - How to Find the Larger or Smaller Value of Sequences at Each Element Index

Contents

1. Introduction
2. Keywords
3. Problema
4. Solution
5. Discussion
5.1 Zip() standard query operator
6. Pratice: Denoting Bidding Values
7. Conclusions
8. Literature & Links

1. Introduction

With this new LINQ recipe the programmer will learn how to find the larger or smaller value from several given sequences. These sequences have the same length and are composed of numeral values. Particularly, the programmer is going to use, once again, the Zip() standard query operator from LINQ to accomplish this computation. In the practice section, the reader will know how to find the maximum and minimum bid values from the given sequences. LINQ is amazing!

2. Keywords

  • Collection
  • LINQ
  • Sequence
  • Standard query operator
  • Zip()

3. Problem

Find the minimum or maximum value of several sequences at each element index.

4. Solution

In LINQ we find the Zip() standard query operator to apply some specified function to a sequence of elements; the function uses each index as input values.

5. Discussion

5.1 Zip() standard query operator

Zip() is a standard query operator; it's useful to apply some specified function to each element index of a sequence. Visually, it can viewed as 
Zip visual operation schema
Illustration 1. Zip() visual operation schema.
Notice that each index from seq1 has its corresponding index on seq2: the function operates over each index.

To exemplify it, this code concatenates the symbol and literal representations for numbers: 

int[] numbers = { 1, 2, 3 };
string[] words = { "One", "Two", "Three"};

var numbersAndWords = numbers.Zip(words, (n, w) =>
String.Format("{0} - {1}", n, w));

numbersAndWords.Dump("Numbers and Words");

Once executed in LINQPad, this is the result: 
Zip() example
Illustration 2. Zip() example.

6. Practice: Denoting Bidding Values

Suppose we have two collections to represent the bidding values for different items.

The purpose with this recipe is to learn how, by means of LINQ, to find the smaller and larger of bidding values.

LINQ file MinMaxBids.cs [Mirror 1][Mirror 2]: 
Lines 2 and 3 define two list of bid values as integer elements. Next, lines 6-8, we call Zip() function to find the maximum bids from sequence's indexes. Observe how the Math.Max() function is used to find the maximum between the two parameters bid1 and bid2.


Analogally, lines 11-13, compute the Math.Min() function to find the minimum of the two parameters -bid1 and bid2.


The output in LINQPad
Minimum and maximum bids
Illustration 1. Minimum and maximum bids.

An extended version of this solution consists on multiple bid sequences (it's a generalized approach): 

LINQ file MinMaxBidsGeneralApproach.cs [Mirror 1][Mirror 2]: 

Four sequences are defined to contain bid values (lines 2-5). All these sequences are added to a list of lists (lines 8-12). Its purpose is to find the smaller and larger bid values using the Aggregate ("Enumerable.Aggregate(TSource) Method", 2016); basically, what this method does is apply an accumulator over a sequence.


Once executed, this is the resultant output in LINQPad
Minimum and maximum bids (general approach)
Illustration 3. Minimum and maximum bids (general approach).
Video tutorial: 

7. Conclusions

We have used the Zip() as programmatic mechanism to find the smaller or larger values from two sequences. This knowledge is useful for many applications which require this kind of computation for some type of values.


Next LINQ recipe is going to explain how to generate Armstrong Numbers and similar number sequences.

8. Literature & Links

Mukherjee, S (2014). Thinking in LINQ Harnessing the Power of Functional Programming in .NET Applications. United States: Apress.
Enumerable.Zip(TFirst, TSecond, TResult) Method (System.Linq) (2016, julio 13). Retrieved from: https://msdn.microsoft.com/en-us/library/dd267698%28v=vs.100%29.aspx?f=255&MSPPError=-2147217396
Enumerable.Aggregate(TSource) Method (IEnumerable(TSource), Func(TSource, TSource, TSource)) (System.Linq) (2016, julio 13). Retrieved from: https://msdn.microsoft.com/en-us/library/bb548651(v=vs.110).aspx


O

No hay comentarios:

Publicar un comentario

Envíe sus comentarios, dudas, sugerencias, críticas. Gracias.