public void ComplexTest() { list.Add(1); list.Add(22); list.Add(-3); list.Add(1); list.Add(0); list.Add(222); list.Add(1); list.Add(-79); list.Add(38); list = Functions.Map(list, x => x - 4); Assert.AreEqual(9, list.GetLength()); Assert.AreEqual(34, list.GetElement(8)); Assert.AreEqual(-83, list.GetElement(7)); Assert.AreEqual(-3, list.GetElement(6)); Assert.AreEqual(218, list.GetElement(5)); Assert.AreEqual(-4, list.GetElement(4)); Assert.AreEqual(-3, list.GetElement(3)); Assert.AreEqual(-7, list.GetElement(2)); Assert.AreEqual(18, list.GetElement(1)); Assert.AreEqual(-3, list.GetElement(0)); list = Functions.Filter(list, x => x % 3 == 0); Assert.AreEqual(4, list.GetLength()); Assert.AreEqual(-3, list.GetElement(3)); Assert.AreEqual(18, list.GetElement(2)); Assert.AreEqual(-3, list.GetElement(1)); Assert.AreEqual(-3, list.GetElement(0)); Assert.AreEqual(991, Functions.Fold(list, 1000, (acc, elem) => acc - elem)); }
/// <summary> /// Filter list function /// </summary> /// <param name="list"> List for checking </param> /// <param name="function"> Function for filter </param> /// <returns> Filtered list </returns> public static List Filter(List list, Func<int, bool> function) { List newList = new List(); for (int i = 0; i < list.GetLength(); ++i) if (function(list.GetElement(i))) newList.Add(list.GetElement(i)); return newList; }
public void FilterTest() { list.Add(0); list.Add(50); list.Add(-2); list.Add(-9999); list = Functions.Filter(list, x => x >= 0); Assert.AreEqual(2, list.GetLength()); Assert.AreEqual(0, list.GetElement(0)); Assert.AreEqual(50, list.GetElement(1)); }
/// <summary> /// Filter list function /// </summary> /// <param name="list"> List for checking </param> /// <param name="function"> Function for filter </param> /// <returns> Filtered list </returns> public static List Filter(List list, Func <int, bool> function) { List newList = new List(); for (int i = 0; i < list.GetLength(); ++i) { if (function(list.GetElement(i))) { newList.Add(list.GetElement(i)); } } return(newList); }
/// <summary> /// Map function for list /// </summary> /// <param name="list"> List for change </param> /// <param name="function"> Changing function </param> /// <returns> Changed list </returns> public static List Map(List list, Func<int, int> function) { List newList = new List(); for (int i = 0; i < list.GetLength(); ++i) newList.Add(function(list.GetElement(i))); return newList; }
/// <summary> /// Fold list function /// </summary> /// <param name="list"> List for calculating </param> /// <param name="startValue"> Start value of calculating </param> /// <param name="function"> Functiong for calculating </param> /// <returns> Result of calculating </returns> public static int Fold(List list, int startValue, Func <int, int, int> function) { for (int i = 0; i < list.GetLength(); ++i) { startValue = function(startValue, list.GetElement(i)); } return(startValue); }
/// <summary> /// Map function for list /// </summary> /// <param name="list"> List for change </param> /// <param name="function"> Changing function </param> /// <returns> Changed list </returns> public static List Map(List list, Func <int, int> function) { List newList = new List(); for (int i = 0; i < list.GetLength(); ++i) { newList.Add(function(list.GetElement(i))); } return(newList); }
/// <summary> /// Fold list function /// </summary> /// <param name="list"> List for calculating </param> /// <param name="startValue"> Start value of calculating </param> /// <param name="function"> Functiong for calculating </param> /// <returns> Result of calculating </returns> public static int Fold(List list, int startValue, Func<int, int, int> function) { for (int i = 0; i < list.GetLength(); ++i) startValue = function(startValue, list.GetElement(i)); return startValue; }