Esempio n. 1
0
        static void Main()
        {
            const int numberOfIntegers = 10, numberOfPeople = 10;

            int[] integers = IntegerList.CreateRandomIntegers(numberOfIntegers, -50, 50);

            Console.Write("Integers: [");
            integers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * Square values
            var squareValues = integers.Select(integer => integer * integer);

            Console.Write("\nSquare values: [");
            squareValues.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * Summation
            int summation  = 0;
            var summations = integers.Select(integer => {
                summation += integer;
                return(summation);
            });

            Console.Write("\nSummations: [");
            summations.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            Person[] people = PersonList.CreateRandomPeople(numberOfPeople);
            Console.WriteLine("\nPeople:");
            people.ForEach(Console.WriteLine);

            // * All of them are one year older
            var grownUp = people.Select(person => person.Birthday());

            Console.WriteLine("\nPeople a year older:");
            grownUp.ForEach(Console.WriteLine);

            // * We take the people's ages
            var ages = people.Select(person => person.Age);

            Console.Write("\nAges: [");
            ages.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * People's incomes
            IDictionary <string, decimal> incomePerIdNumber = PersonList.CreateRandomIncomePerPerson(people);
            var incomes = people.Select(person => incomePerIdNumber[person.IDNumber]);

            Console.Write("\nIncomes: [");
            incomes.ForEach(Console.Write, income => Console.Write(", "));
            Console.WriteLine("]");
        }
Esempio n. 2
0
        static void Main()
        {
            const int numberOfIntegers = 10, numberOfPeople = 10;

            int[] integers = IntegerList.CreateRandomIntegers(numberOfIntegers, -50, 50);

            Console.Write("Integers: [");
            integers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * Summation and arithmetic mean
            int summation = integers.Aggregate((e1, e2) => e1 + e2);

            Console.WriteLine("Summation: {0}\nAritmetic mean: {1}",
                              summation, summation / (double)numberOfIntegers);

            // * An initial value (seed) can be passed
            int zero = integers.Aggregate(-summation, (e1, e2) => e1 + e2);

            Console.WriteLine("-Summation + Summation: {0}", zero);

            // * When performing subtraction, we have to be careful, because
            //   the - operator is not associative => (1 - 2) - 1 = -2 <> 1 - (2 - 1) = 0
            // * Therefore, the iteration order matters (left to right <> right to left)
            // * From left to right
            int leftToRight = integers.Aggregate((e1, e2) => e1 - e2);

            Console.WriteLine("Left to right subtraction: {0}", leftToRight);
            // * Right to left:
            int rightToLeft = integers.Reverse().Aggregate((e1, e2) => e1 - e2);

            Console.WriteLine("Right to left subtraction: {0}", rightToLeft);

            // * We compute the average income
            Person[] people = PersonList.CreateRandomPeople(numberOfPeople);
            IDictionary <string, decimal> incomePerIdNumber = PersonList.CreateRandomIncomePerPerson(people);
            var incomeSummation = people
                                  .Select(person => incomePerIdNumber[person.IDNumber])
                                  .Aggregate((income1, income2) => income1 + income2);

            Console.WriteLine("Average income: {0:N} euros.", incomeSummation / people.Length);
        }
Esempio n. 3
0
        static void Main()
        {
            const int numberOfIntegers = 100, numberOfPeople = 30;

            int[]    integers = IntegerList.CreateRandomIntegers(numberOfIntegers);
            Person[] people   = PersonList.CreateRandomPeople(numberOfPeople);

            Console.Write("Integers: [");
            integers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            Console.WriteLine("\nPeople:");
            people.ForEach(Console.WriteLine);

            // * Example use of Filter (Where in Linq)
            // * Even numbers
            var evenNumbers = integers.Where(integer => integer % 2 == 0);

            Console.Write("\nEven numbers: [");
            evenNumbers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * Positive odd numbers
            var positiveOddNumbers = integers.Where(integer => integer % 2 != 0 && integer > 0);

            Console.Write("\nPositive odd numbers: [");
            positiveOddNumbers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * People 18 and beyond
            var beyond18 = people.Where(person => person.Age >= 18);

            Console.WriteLine("\nPeople 18 and beyond:");
            beyond18.ForEach(Console.WriteLine);

            // * People with a specific name
            Random random             = new Random();
            string name               = PersonList.FirstNames[random.Next(PersonList.FirstNames.Length)];
            var    peopleSpecificName = people.Where(person => person.FirstName.Equals(name));

            Console.WriteLine("\nPeople named \"{0}\":", name);
            peopleSpecificName.ForEach(Console.WriteLine);

            // * A new list of integers
            integers = IntegerList.CreateRandomIntegers(numberOfIntegers, -50, 50);
            Console.Write("Integers: [");
            integers.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");

            // * Computes the set of duplicated numbers (only one ocurrence per duplicated number)
            // * processed stores the integers that were processed
            // * duplicated stores the integers that were already detected as duplicated
            // * setOfDuplicated is like duplictated, but a set (a single occurrence per number)
            IList <int> processed = new List <int>(), duplicated = new List <int>();
            var         setOfDuplicated = integers.Where(
                integer => {
                if (processed.Contains(integer))
                {
                    if (duplicated.Contains(integer))
                    {
                        return(false);    // Already included in duplicated
                    }
                    duplicated.Add(integer);
                    return(true);    // It is a duplicated number
                }
                processed.Add(integer);
                return(false);    // It is not duplicated (by now)
            }
                );

            Console.Write("\nDuplicated values: [");
            setOfDuplicated.ForEach(Console.Write, integer => Console.Write(", "));
            Console.WriteLine("]");
        }