diff --git a/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs new file mode 100644 index 0000000..5435166 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; +using System.Collections.Generic; +using System.Text; + +namespace hw6MapFilterFold.Test +{ + public class Tests + { + private List listInt; + + private List listString; + + private List listChar; + + [SetUp] + public void Setup() + { + listInt = new List() { 1, 2, 3 }; + listString = new List() { "a", "b", "c" }; + listChar = new List() { 'a', 'b', 'c' }; + } + + [Test] + public void MapTest() + { + var resultList1 = new List() { 2, 4, 6 }; + var list1 = Functions.Map(listInt, x => x * 2); + Assert.AreEqual(resultList1, list1); + var resultList2 = new List() { "aaaa", "baaa", "caaa" }; + var list2 = Functions.Map(listString, x => x + "aaa"); + Assert.AreEqual(resultList2, list2); + var resultList3 = new List() { 'd', 'd', 'd' }; + var list3 = Functions.Map(listChar, x => 'd'); + Assert.AreEqual(resultList3, list3); + } + + [Test] + public void FilterTest() + { + var resultList1 = new List() { 2 }; + var list1 = Functions.Filter(listInt, y => y % 2 == 0 ); + Assert.AreEqual(resultList1, list1); + var resultList2 = new List() { "b" }; + var list2 = Functions.Filter(listString, x => x == "b"); + Assert.AreEqual(resultList2, list2); + var resultList3 = new List() { }; + var list3 = Functions.Filter(listChar, x => x == 'w'); + Assert.AreEqual(resultList3, list3); + } + + [Test] + public void FoldTest() + { + var fold1 = Functions.Fold(listInt, 1, (acc, elem) => acc * elem); + Assert.AreEqual(6, fold1); + var fold2 = Functions.Fold(listString, "w", (acc, elem) => acc + elem); + Assert.AreEqual("wabc", fold2); + } + } +} \ No newline at end of file diff --git a/hw6MapFilterFold/hw6MapFilterFold.Test/hw6MapFilterFold.Test.csproj b/hw6MapFilterFold/hw6MapFilterFold.Test/hw6MapFilterFold.Test.csproj new file mode 100644 index 0000000..7090d25 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold.Test/hw6MapFilterFold.Test.csproj @@ -0,0 +1,19 @@ + + + + net5.0 + + false + + + + + + + + + + + + + diff --git a/hw6MapFilterFold/hw6MapFilterFold.sln b/hw6MapFilterFold/hw6MapFilterFold.sln new file mode 100644 index 0000000..43815c1 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31205.134 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6MapFilterFold", "hw6MapFilterFold\hw6MapFilterFold.csproj", "{B613C0DE-5E5B-4D24-9DE9-BB8722368CAA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "hw6MapFilterFold.Test", "hw6MapFilterFold.Test\hw6MapFilterFold.Test.csproj", "{D7D8280D-1D14-410B-8EDA-18323E4D7EE2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B613C0DE-5E5B-4D24-9DE9-BB8722368CAA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B613C0DE-5E5B-4D24-9DE9-BB8722368CAA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B613C0DE-5E5B-4D24-9DE9-BB8722368CAA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B613C0DE-5E5B-4D24-9DE9-BB8722368CAA}.Release|Any CPU.Build.0 = Release|Any CPU + {D7D8280D-1D14-410B-8EDA-18323E4D7EE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7D8280D-1D14-410B-8EDA-18323E4D7EE2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7D8280D-1D14-410B-8EDA-18323E4D7EE2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7D8280D-1D14-410B-8EDA-18323E4D7EE2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6165ECF3-80AF-4BE1-8642-7C9A0F30F522} + EndGlobalSection +EndGlobal diff --git a/hw6MapFilterFold/hw6MapFilterFold/Functions.cs b/hw6MapFilterFold/hw6MapFilterFold/Functions.cs new file mode 100644 index 0000000..035e5bc --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold/Functions.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace hw6MapFilterFold +{ + /// + /// класс с функциями для списка + /// + public static class Functions + { + /// + /// Возвращается список, полученный применением переданной функции к каждому элементу переданного списка + /// + /// список + /// переданная функция + /// новый список + public static List Map(List list, Func function) + { + var resultList = new List(); + foreach (var node in list) + { + resultList.Add(function(node)); + } + return resultList; + } + + /// + /// Возвращается список из элементов переданного списка, для которых переданная функция вернула true + /// + /// список + /// булевая функция + /// новый список + public static List Filter(List list, Func function) + { + var returnList = new List(); + foreach (var node in list) + { + if (function(node)) + { + returnList.Add(node); + } + } + return returnList; + } + + /// + /// возвращает накопленное значение, получившееся после всего прохода списка + /// + /// список + /// начальное значение + /// функция + /// накопленное значение + public static TResult Fold(List list, TResult startValue,Func function) + { + foreach (var node in list) + { + startValue = function(startValue, node); + } + return startValue; + } + } +} \ No newline at end of file diff --git a/hw6MapFilterFold/hw6MapFilterFold/Program.cs b/hw6MapFilterFold/hw6MapFilterFold/Program.cs new file mode 100644 index 0000000..a2b1853 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; + +namespace hw6MapFilterFold +{ + class Program + { + static void Main(string[] args) + { + var listInt = new List() { 1, 2, 3 }; + var resultList1 = new List() { 2, 4, 6 }; + var list1 = Functions.Map(listInt, x => x * 2); + + var listString = new List() { "a", "b", "c" }; + var resultList2 = new List() { "aaaa", "baaa", "caaa" }; + var list2 = Functions.Map(listString, x => x + "aaa"); + + var listChar = new List() { 'a', 'b', 'c' }; + var resultList3 = new List() { 'd', 'd', 'd' }; + var list3 = Functions.Map(listChar, x => 'd'); + + } + } +} \ No newline at end of file diff --git a/hw6MapFilterFold/hw6MapFilterFold/hw6MapFilterFold.csproj b/hw6MapFilterFold/hw6MapFilterFold/hw6MapFilterFold.csproj new file mode 100644 index 0000000..2082704 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold/hw6MapFilterFold.csproj @@ -0,0 +1,8 @@ + + + + Exe + net5.0 + + +