public IntList Filter(IntPredicate p) { var res = new IntList(); foreach (var i in this) { if (p(i)) { res.Add(i); } } return(res); }
public IntList greaterThan25(IntPredicate p) { IntList res = new IntList(); foreach (int i in this) { if (p(i)) { res.Add(i); } } return(res); }
public static void Main(String[] args) { var xs = new IntList { 1, 122, 3, 4, 5 }; xs.Act(Console.WriteLine); Console.WriteLine("Print only even numbers:"); xs.Filter(delegate(int x) { return(x % 2 == 0); }).Act(Console.WriteLine); Console.WriteLine("Using anonymous methods: (x >= 25) "); xs.Act(delegate(int x) { if (x >= 25) { Console.WriteLine(x); } }); // xs.Filter(delegate (int x) { return x >= 25; }).Act(Console.WriteLine); Console.WriteLine("Computing sum: "); int sum = 0; xs.Act(delegate(int x) { sum += x; }); Console.WriteLine(sum); //Generic Console.WriteLine("Generics"); var gs = new MyList <int> { 1, 122, 3, 4, 5 }; gs.Act(Console.WriteLine); Console.WriteLine("Print only even numbers:"); gs.Filter(delegate(int x) { return(x % 2 == 0); }).Act(Console.WriteLine); Console.WriteLine("Using anonymous methods: (x >= 25) "); gs.Act(delegate(int x) { if (x >= 25) { Console.WriteLine(x); } }); // gs.Filter(delegate (int x) { return x >= 25; }).Act(Console.WriteLine); Console.WriteLine("Computing sum: "); int sumg = 0; gs.Act(delegate(int x) { sumg += x; }); Console.WriteLine(sum); Console.ReadKey(); }
static void Main(string[] args) { IntList xs = new IntList(2, 4, 7, 9, 13); IntList xs1 = new IntList(25, 30, 1, 35, 0); xs.Act(Console.WriteLine); xs.Filter(delegate(int x) { return(x % 2 == 0); }).Act(Console.WriteLine); xs1.Filter(delegate(int x) { return(x > 25); }).Act(Console.WriteLine); int sum = 0; xs1.Act(p => sum += p); Console.WriteLine(sum); Console.ReadKey(); }
public static string ToString <T>(this IntList list) { var sb = new StringBuilder("["); list.ForEach(item => { if (item.Equals(list.Last())) { sb.Append(item + "]"); } else { sb.Append(item + ", "); } }); return(sb.ToString()); }
static void Main(string[] args) { IntList xs = new IntList(); xs.Add(11); xs.Add(20); xs.Add(31); xs.Add(40); xs.Add(51); xs.Add(71); xs.Add(80); xs.Add(91); xs.Act(Console.WriteLine); Console.WriteLine(" "); Console.WriteLine("Elements divisible by 2. "); xs.Filter(delegate(int x) { return(x % 2 == 0); }).Act(Console.WriteLine); Console.WriteLine(" "); Console.WriteLine("Elements greater than 25. "); xs.Filter(y => y > 25).Act(Console.WriteLine); Console.ReadKey(); }
static void Main(string[] args) { var xs = new IntList(1, 2, 3, 4, 500, 100); #region IntList Console.WriteLine("\n------------ IntList ------------"); Console.WriteLine("\nPrint all elements"); xs.Act(Console.WriteLine); Console.WriteLine("\nCustom print all elements"); xs.Act(CustomWriteLine); Console.WriteLine("\nPrint all even elements"); xs.Filter(x => x % 2 == 0).Act(Console.WriteLine); Console.WriteLine("\nPrint all even elements that are greater than or equal to 25"); xs.Filter(x => x >= 25).Act(Console.WriteLine); xs.Act(AddElements); Console.WriteLine($"\nSum of {xs.ToString<int>()} is {_count}"); #endregion #region Generic List Console.WriteLine("\n------------ Generic List ------------"); var stringList = new MyList <string>("Sara", "Jonh", "Maria"); Console.WriteLine("\nPrint all elements"); stringList.Act(Console.WriteLine); Console.WriteLine("\nCustom print all elements"); stringList.Act(CustomWriteLine); Console.WriteLine("\nPrint all elements with more than 4 leters"); stringList.Filter(x => x.Length > 4).Act(Console.WriteLine); #endregion Console.ReadLine(); }