using System;
using System.Collections.Generic;
using System.Linq;
namespace Linq.Recipes.Ch01
{
public class TypeFunctionsExamples
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("--- Generator Functions ---");
Console.WriteLine("- Enumerable.Range -");
IEnumerable<int> cubes = Enumerable.Range(1,5).Select(num => num * num);
Console.WriteLine("Powers of 3 (between 1 and 5): ");
foreach(int cube in cubes)
{
Console.Write("{0} ", cube);
}
Console.WriteLine("\n");
Console.WriteLine("--- Statistical Functions ---");
Console.WriteLine("- Enumerable.Count -");
string[] msProducts = {"Windows", "Office", "Flight Simulator", "Minecraft", "Edge"};
int numberOfFruits = msProducts.Count();
Console.WriteLine("Number of (some) Microsoft products: {0}", numberOfFruits);
Console.WriteLine("\n");
Console.WriteLine("--- Projector Functions ---");
Console.WriteLine("- Enumerable.Select -");
IEnumerable<int> squares = Enumerable.Range(1, 5).Select(num => num * num);
Console.WriteLine("Powers of 2 (between 1 and 5): ");
foreach(int square in squares)
{
Console.Write("{0} ", square);
}
Console.WriteLine("\n");
Console.WriteLine("--- Filter Functions ---");
Console.WriteLine("- Enumerable.First -");
Console.WriteLine("Microsoft product: {0}", msProducts.First());
Console.WriteLine ();
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CgpuYW1lc3BhY2UgTGlucS5SZWNpcGVzLkNoMDEKewogICAgcHVibGljIGNsYXNzIFR5cGVGdW5jdGlvbnNFeGFtcGxlcwogICAgewogICAgICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBNYWluKCkgCiAgICAgICAgewogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiLS0tIEdlbmVyYXRvciBGdW5jdGlvbnMgLS0tIik7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCItIEVudW1lcmFibGUuUmFuZ2UgLSIpOwogICAgICAgICAgICBJRW51bWVyYWJsZTxpbnQ+IGN1YmVzID0gRW51bWVyYWJsZS5SYW5nZSgxLDUpLlNlbGVjdChudW0gPT4gbnVtICogbnVtKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIlBvd2VycyBvZiAzIChiZXR3ZWVuIDEgYW5kIDUpOiAiKTsKICAgICAgICAgICAgZm9yZWFjaChpbnQgY3ViZSBpbiBjdWJlcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZSgiezB9ICIsIGN1YmUpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIAogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG4iKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIi0tLSBTdGF0aXN0aWNhbCBGdW5jdGlvbnMgLS0tIik7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCItIEVudW1lcmFibGUuQ291bnQgLSIpOwogICAgICAgICAgICBzdHJpbmdbXSBtc1Byb2R1Y3RzID0geyJXaW5kb3dzIiwgIk9mZmljZSIsICJGbGlnaHQgU2ltdWxhdG9yIiwgIk1pbmVjcmFmdCIsICJFZGdlIn07CiAgICAgICAgICAgIGludCBudW1iZXJPZkZydWl0cyA9IG1zUHJvZHVjdHMuQ291bnQoKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIk51bWJlciBvZiAoc29tZSkgTWljcm9zb2Z0IHByb2R1Y3RzOiB7MH0iLCBudW1iZXJPZkZydWl0cyk7CiAgICAgICAgICAgIAogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiXG4iKTsKICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUoIi0tLSBQcm9qZWN0b3IgRnVuY3Rpb25zIC0tLSIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiLSBFbnVtZXJhYmxlLlNlbGVjdCAtIik7CiAgICAgICAgICAgIElFbnVtZXJhYmxlPGludD4gc3F1YXJlcyA9IEVudW1lcmFibGUuUmFuZ2UoMSwgNSkuU2VsZWN0KG51bSA9PiBudW0gKiBudW0pOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiUG93ZXJzIG9mIDIgKGJldHdlZW4gMSBhbmQgNSk6ICIpOwogICAgICAgICAgICBmb3JlYWNoKGludCBzcXVhcmUgaW4gc3F1YXJlcykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZSgiezB9ICIsIHNxdWFyZSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgCiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJcbiIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiLS0tIEZpbHRlciBGdW5jdGlvbnMgLS0tIik7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCItIEVudW1lcmFibGUuRmlyc3QgLSIpOwogICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiTWljcm9zb2Z0IHByb2R1Y3Q6IHswfSIsIG1zUHJvZHVjdHMuRmlyc3QoKSk7CiAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lICgpOwogICAgICAgIH0KICAgIH0KfQ==