From 0e423f5d010feeecf537fcd14d1335743408a03a Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Sat, 17 Apr 2021 02:56:47 +0300 Subject: [PATCH 1/2] add function and test --- .../MapFilterFoldTest.cs | 61 +++++++++++++++++++ .../hw6MapFilterFold.Test.csproj | 19 ++++++ hw6MapFilterFold/hw6MapFilterFold.sln | 31 ++++++++++ .../hw6MapFilterFold/Functions.cs | 44 +++++++++++++ hw6MapFilterFold/hw6MapFilterFold/Program.cs | 24 ++++++++ .../hw6MapFilterFold/hw6MapFilterFold.csproj | 8 +++ 6 files changed, 187 insertions(+) create mode 100644 hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs create mode 100644 hw6MapFilterFold/hw6MapFilterFold.Test/hw6MapFilterFold.Test.csproj create mode 100644 hw6MapFilterFold/hw6MapFilterFold.sln create mode 100644 hw6MapFilterFold/hw6MapFilterFold/Functions.cs create mode 100644 hw6MapFilterFold/hw6MapFilterFold/Program.cs create mode 100644 hw6MapFilterFold/hw6MapFilterFold/hw6MapFilterFold.csproj diff --git a/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs new file mode 100644 index 0000000..8c14def --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs @@ -0,0 +1,61 @@ +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..01ddf97 --- /dev/null +++ b/hw6MapFilterFold/hw6MapFilterFold/Functions.cs @@ -0,0 +1,44 @@ +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) + { + int count = list.Count; + var resultList = new List(); + foreach (var node in list) + { + resultList.Add(function(node)); + } + return resultList; + } + + 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 T Fold(List list, T 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..185992c --- /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 + + + From a43050be181394066e5d932622627133d1720a18 Mon Sep 17 00:00:00 2001 From: MikePuzanov <89803250877@mail.ru> Date: Fri, 4 Jun 2021 13:27:23 +0300 Subject: [PATCH 2/2] fix mistakes --- .../MapFilterFoldTest.cs | 17 +++++---- .../hw6MapFilterFold/Functions.cs | 35 +++++++++++++++---- hw6MapFilterFold/hw6MapFilterFold/Program.cs | 6 ++-- 3 files changed, 39 insertions(+), 19 deletions(-) diff --git a/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs index 8c14def..5435166 100644 --- a/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs +++ b/hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs @@ -24,13 +24,13 @@ public void Setup() public void MapTest() { var resultList1 = new List() { 2, 4, 6 }; - var list1 = Functions.Map(listInt, x => x * 2); + 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"); + 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'); + var list3 = Functions.Map(listChar, x => 'd'); Assert.AreEqual(resultList3, list3); } @@ -38,23 +38,22 @@ public void MapTest() public void FilterTest() { var resultList1 = new List() { 2 }; - var list1 = Functions.Filter(listInt, y => y % 2 == 0 ); + 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"); + var list2 = Functions.Filter(listString, x => x == "b"); Assert.AreEqual(resultList2, list2); var resultList3 = new List() { }; - var list3 = Functions.Filter(listChar, x => x == 'w'); + 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); + var fold1 = Functions.Fold(listInt, 1, (acc, elem) => acc * elem); Assert.AreEqual(6, fold1); - var fold2 = Functions.Fold(listString, "w", (acc, elem) => acc + elem); + var fold2 = Functions.Fold(listString, "w", (acc, elem) => acc + elem); Assert.AreEqual("wabc", fold2); } } diff --git a/hw6MapFilterFold/hw6MapFilterFold/Functions.cs b/hw6MapFilterFold/hw6MapFilterFold/Functions.cs index 01ddf97..035e5bc 100644 --- a/hw6MapFilterFold/hw6MapFilterFold/Functions.cs +++ b/hw6MapFilterFold/hw6MapFilterFold/Functions.cs @@ -6,12 +6,20 @@ namespace hw6MapFilterFold { - public static class Functions + /// + /// класс с функциями для списка + /// + public static class Functions { - public static List Map(List list, Func function) + /// + /// Возвращается список, полученный применением переданной функции к каждому элементу переданного списка + /// + /// список + /// переданная функция + /// новый список + public static List Map(List list, Func function) { - int count = list.Count; - var resultList = new List(); + var resultList = new List(); foreach (var node in list) { resultList.Add(function(node)); @@ -19,9 +27,15 @@ public static List Map(List list, Func function) return resultList; } - public static List Filter(List list, Func function) + /// + /// Возвращается список из элементов переданного списка, для которых переданная функция вернула true + /// + /// список + /// булевая функция + /// новый список + public static List Filter(List list, Func function) { - var returnList = new List(); + var returnList = new List(); foreach (var node in list) { if (function(node)) @@ -32,7 +46,14 @@ public static List Filter(List list, Func function) return returnList; } - public static T Fold(List list, T startValue,Func function) + /// + /// возвращает накопленное значение, получившееся после всего прохода списка + /// + /// список + /// начальное значение + /// функция + /// накопленное значение + public static TResult Fold(List list, TResult startValue,Func function) { foreach (var node in list) { diff --git a/hw6MapFilterFold/hw6MapFilterFold/Program.cs b/hw6MapFilterFold/hw6MapFilterFold/Program.cs index 185992c..a2b1853 100644 --- a/hw6MapFilterFold/hw6MapFilterFold/Program.cs +++ b/hw6MapFilterFold/hw6MapFilterFold/Program.cs @@ -9,15 +9,15 @@ 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 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 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'); + var list3 = Functions.Map(listChar, x => 'd'); } }