static void Main1(string[] args) { // lambda DoubleIt db = x => x * 2; Console.WriteLine($"4*2 = {db(4)}"); // lambda with list List <int> nlist = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var impaireList = nlist.Where(x => x % 2 != 0).ToList(); foreach (var v in impaireList) { Console.WriteLine(v); } // get a rang of value with lambda x<3>8 var rangList = nlist.Where(x => (x > 2) && (x < 8)).ToList(); foreach (var v in rangList) { Console.WriteLine(v); } // random add element to list List <int> intlist = new List <int>(); int i = 0; Random rdn = new Random(); while (i < 100) { intlist.Add(rdn.Next(1, 4)); i++; } Console.WriteLine("equal to 1 : {0}", intlist.Where(x => x == 1).ToList().Count); Console.WriteLine("equal to 2 : {0}", intlist.Where(x => x == 2).ToList().Count); Console.WriteLine("equal to 3 : {0}", intlist.Where(x => x == 3).ToList().Count); Console.WriteLine("equal to 4 : {0}", intlist.Where(x => x == 4).ToList().Count); // lambda with string type List <string> stringlist = new List <string> { "baddi", " youssef", "Adil", " Aymen" }; var aNameStrList = stringlist.Where(x => x.ToLower().Trim().StartsWith("a")).ToList(); foreach (var v in aNameStrList) { Console.WriteLine(v); } Console.ReadLine(); }
public static void Main1() { // ecriture lambda DoubleIt db = x => x * 2; // lambda avec les list List <int> nlist = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var impaireList = nlist.Where(x => x % 2 != 0).ToList(); foreach (var v in impaireList) { Console.WriteLine(v); } // get a rang of value with lambda var rangList = nlist.Where(x => (x > 2) && (x < 8)).ToList(); foreach (var v in rangList) { Console.WriteLine(v); } // lambda avec des string type List <string> strList = new List <string> { "baddi", "adil", " Aymen", "youssef" }; var aNameStrList = strList.Where(x => x.ToLower().Trim().StartsWith("a")).ToList(); foreach (var v in aNameStrList) { Console.WriteLine(v); } }
public static void Sample(string[] args) { //lambda function //single line : same as JavaScript DoubleIt dblIt = x => Console.WriteLine($"Double of {x} is : {x * 2}"); //multipleLine Approach QubeIt qbIt = q => { var p = 3; return(Math.Pow(q, p)); }; dblIt(5); Console.WriteLine("Qube of {0} is {1}", 6, qbIt(6)); //use lambda as a callback like js List <int> intArrrays = new List <int> { 4, 6, 9, 1, 5 }; var greaterThanFours = intArrrays.Where(x => x > 4); foreach (object obj in greaterThanFours) { Console.WriteLine($"Greate than 4 : {obj}"); } //sort using lambda like js intArrrays.Sort((x, y) => x > y ? 1 : -1); Console.WriteLine("After Sort.."); Console.WriteLine(string.Join(",", intArrrays)); //sort list of objects using lambda List <IProcessExample> processList = new List <IProcessExample>(); processList.Add(new IProcessExample() { ProcessName = "Process1", ArrivalTime = 2, BurstTime = 5 }); processList.Add(new IProcessExample() { ProcessName = "Process2", ArrivalTime = 2, BurstTime = 5 }); processList.Add(new IProcessExample() { ProcessName = "Process3", ArrivalTime = 1, BurstTime = 5 }); processList.Add(new IProcessExample() { ProcessName = "Process4", ArrivalTime = 3, BurstTime = 9 }); processList.Sort((p1, p2) => { return(p1.BurstTime > p2.BurstTime ? p1.ArrivalTime > p2.ArrivalTime ? 1 : -1 : -1); }); Console.WriteLine("Process execution queue..."); foreach (IProcessExample obj in processList) { Console.WriteLine($"ProcessName : {obj.ProcessName} \n" + $"Arrival Time : {obj.ArrivalTime} \n" + $"Burst Time : {obj.BurstTime}"); Console.WriteLine(); } //Counts //Head = 1, Tail = 2 int i = 0; List <int> randomIntegers = new List <int>(); Random rnd = new Random(); while (i < 100) { randomIntegers.Add(rnd.Next(1, 3)); i++; } Console.WriteLine("Number of Heads is : {0}", randomIntegers.Count(x => x == 1)); Console.WriteLine("Number of Tailss is : {0}", randomIntegers.Count(x => x == 2)); //Select: behaves almost like js map List <int> oneTo10 = new List <int>(); oneTo10.AddRange(Enumerable.Range(1, 10)); var oneTo10Squares = oneTo10.Select(x => Math.Pow(x, 2)); Console.WriteLine("Square of 1 to 10 are: {0}", string.Join(", ", oneTo10Squares)); //Zip List <int> a1 = new List <int>() { 1, 2, 3 }; List <int> a2 = new List <int>() { 4, 5, 6 }; var zippedAs = a1.Zip(a2).ToList(); //will return a pair of values Console.WriteLine("Zipped result of a1 and a2 is : {0}", string.Join(", ", zippedAs)); var zippedAs2 = a1.Zip(a2, (x, y) => x + y).ToList(); //will return the zipped addition Console.WriteLine("Zipped result of a1 and a2 is : {0}", string.Join(", ", zippedAs2)); //Aggregate: behaves like reduce Console.WriteLine("Total of a1 is : {0}", a1.Aggregate((a, b) => a + b)); //Other libraries Console.WriteLine("Average of a2 is : {0}", a2.AsQueryable().Average()); Console.WriteLine("All > 3 : {0}", a1.All(x => x > 3)); Console.WriteLine("Any > 3 : {0}", a2.Any(x => x > 3)); //getting unique values Console.WriteLine("Different elements of oneTo10 with respect to a1: {0}", string.Join(",", oneTo10.Except(a1))); }
static void Main(string[] args) { //Lambda //input params in left //code to execute on the right DoubleIt wham = (x => x * 2); Console.WriteLine("Simple lambda delegate(Wham): {0}", wham(5)); //Search all even numbers in a numlist //using lambda List <int> numList = new List <int>() { 1, 3, 4, 6, 2, 8, 6 }; Console.WriteLine("All even numbers in numList: "); var evenList = numList.Where(x => x % 2 == 0).ToList <int>(); foreach (int i in evenList) { Console.WriteLine(i); } //range example var rangelist = numList.Where(x => (x > 3) && (6 >= x)).ToList <int>(); Console.WriteLine("Numbers between 3 and 6(inclusive of 6): {0}", string.Join(", ", rangelist)); //find the number of heads and tails Random r = new Random(); List <int> flipList = new List <int>(); for (int i = 0; i < 100; i++) { flipList.Add(r.Next(1, 3)); } Console.WriteLine(); Console.WriteLine("Head count: {0}", flipList.Where(x => x == 1).ToList().Count()); Console.WriteLine("Tail count: {0}", flipList.Where(x => x == 2).ToList().Count()); //find all name starting with 's' var nameList = new List <string>() { "Manta", "Chino", "Saxony", "Sandy", "Boss" }; Console.WriteLine("Names starting with s: {0}", string.Join(" ", nameList.Where(x => x.StartsWith("s", StringComparison.OrdinalIgnoreCase)).ToList())); //SELECT //executes function on each item in list. var ten = new List <int>(); ten.AddRange(Enumerable.Range(1, 10)); var squares = ten.Select(x => x * x); Console.WriteLine("Squares using select: {0}", string.Join(" ", squares)); //ZIP //applies function to two lists //example, add 2 lists together var one = new List <int>() { 1, 3, 5 }; var two = new List <int>() { 2, 4 }; //takes as length the list with the less count of items //output: 3 7 Console.WriteLine("Sum of one and two: {0}", string.Join(" ", one.Zip(two, (x, y) => x + y).ToList())); Console.WriteLine(); //AGGREGATE //performs operation on each item in a list //carries result forward var numList2 = new List <int>() { 1, 2, 3, 4, 5 }; Console.WriteLine("Sum of one and two: {0}", string.Join(" ", numList2.Aggregate((x, y) => x + y))); Console.WriteLine(); #region derek code // ---------- AVERAGE ---------- // Get the average of a list of values var numList3 = new List <int>() { 1, 2, 3, 4, 5 }; // AsQueryable allows you to manipulate the // collection with the Average function Console.WriteLine("AVG : {0}", numList3.AsQueryable().Average()); // ---------- ALL ---------- // Determines if all items in a list // meet a condition var numList4 = new List <int>() { 1, 2, 3, 4, 5 }; Console.WriteLine("All > 3 : {0}", numList4.All(x => x > 3)); // ---------- ANY ---------- // Determines if any items in a list // meet a condition var numList5 = new List <int>() { 1, 2, 3, 4, 5 }; Console.WriteLine("Any > 3 : {0}", numList5.Any(x => x > 3)); // ---------- DISTINCT ---------- // Eliminates duplicates from a list var numList6 = new List <int>() { 1, 2, 3, 2, 3 }; Console.WriteLine("Distinct : {0}", string.Join(", ", numList6.Distinct())); // ---------- EXCEPT ---------- // Receives 2 lists and returns values not // found in the 2nd list var numList7 = new List <int>() { 1, 2, 3, 2, 3 }; var numList8 = new List <int>() { 3 }; Console.WriteLine("Except : {0}", string.Join(", ", numList7.Except(numList8))); // ---------- INTERSECT ---------- // Receives 2 lists and returns values that // both lists have var numList9 = new List <int>() { 1, 2, 3, 2, 3 }; var numList10 = new List <int>() { 2, 3 }; Console.WriteLine("Intersect : {0}", string.Join(", ", numList9.Intersect(numList10))); #endregion Console.ReadLine(); }
static void Main1(string[] args) { // lambda DoubleIt db = x => x * 2; Console.WriteLine($"4*2 = {db(4)}"); #region Where // lambda with list List <int> nlist = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var impaireList = nlist.Where(x => x % 2 != 0).ToList(); foreach (var v in impaireList) { Console.WriteLine(v); } // get a rang of value with lambda x<3>8 var rangList = nlist.Where(x => (x > 2) && (x < 8)).ToList(); foreach (var v in rangList) { Console.WriteLine(v); } // random add element to list List <int> intlist = new List <int>(); int i = 0; Random rdn = new Random(); while (i < 100) { intlist.Add(rdn.Next(1, 4)); i++; } Console.WriteLine("equal to 1 : {0}", intlist.Where(x => x == 1).ToList().Count); Console.WriteLine("equal to 2 : {0}", intlist.Where(x => x == 2).ToList().Count); Console.WriteLine("equal to 3 : {0}", intlist.Where(x => x == 3).ToList().Count); Console.WriteLine("equal to 4 : {0}", intlist.Where(x => x == 4).ToList().Count); // lambda with string type List <string> stringlist = new List <string> { "baddi", " youssef", "Adil", " Aymen" }; var aNameStrList = stringlist.Where(x => x.ToLower().Trim().StartsWith("a")).ToList(); foreach (var v in aNameStrList) { Console.WriteLine(v); } #endregion #region Select // select on each element in a list with a lambda function Console.WriteLine("##########################"); // add a list from 1 to 10 var blist = new List <int>(); blist.AddRange(Enumerable.Range(1, 10)); var bsquareList = blist.Select(x => Math.Pow(x, 2)); foreach (var v in bsquareList) { Console.WriteLine(v); } #endregion #region Zip // ZIP var clist = new List <int>() { 1, 3, 4, 8, 4, 3 }; // List<int> dlist = null // fire argumnentNullException var dlist = new List <int>() { 5, 6, 7, 4, 8 }; var sumList = clist.Zip(dlist, (x, y) => x + y).ToList(); foreach (var v in sumList) { Console.WriteLine(v); } var c1list = new List <string>() { "1", "3", "4" }; var d1list = new List <string>() { "5", "6", "7" }; var sum1List = clist.Zip(dlist, (x, y) => $"{x} {y}").ToList(); foreach (var v in sum1List) { Console.WriteLine(v); } var sum2List = clist.Where(x => dlist.Contains(x)).ToList().Select(x => x * x); // .Zip(c1list, (x, y) => x + y).ToList(); foreach (var v in sum2List) { Console.WriteLine(v); } // zip entre les deux list where les element sont // Aggregation // an operation on each item in a list and forward the result Console.WriteLine(" Aggregation : {0}", clist.Aggregate((a, b) => a + b)); // AVG Console.WriteLine(" AVG : {0}", clist.AsQueryable().Average()); // ALL Console.WriteLine(" All : {0}", clist.All(x => x > 3)); Console.WriteLine(" All : {0}", clist.All(x => x < 9)); // // Any Console.WriteLine(" All : {0}", clist.Any(x => x > 3)); Console.WriteLine(" All : {0}", clist.Any(x => x > 9)); // Distinct Console.WriteLine(" Dist : {0}", string.Join(",", clist.Distinct())); clist = clist.Distinct().ToList(); foreach (var v in clist) { Console.WriteLine(v); } //Expect // receive two list and return veluess not found in the second list Console.WriteLine(" Dist : {0}", string.Join(",", clist.Except(clist))); // INTERSEC // receive two list and return velues // found in the second list Console.WriteLine(" Dist : {0}", string.Join(",", clist.Intersect(dlist))); #endregion Console.ReadLine(); }