From dae6ebefbe3495a39971d432f8d6eb03264418fa Mon Sep 17 00:00:00 2001 From: Artem Date: Tue, 11 Apr 2023 23:14:05 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=93=D0=BE=D1=82=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=20=D1=81=20=D0=B3=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D1=80=D0=B8=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FilterFoldMap/FilterFoldMap.sln | 25 ++++++++++ FilterFoldMap/FilterFoldMap/Filter.cs | 31 +++++++++++++ .../FilterFoldMap/FilterFoldMap.csproj | 10 ++++ FilterFoldMap/FilterFoldMap/Fold.cs | 28 +++++++++++ FilterFoldMap/FilterFoldMap/Map.cs | 28 +++++++++++ FilterFoldMap/FilterFoldMap/Program.cs | 9 ++++ .../TestsForFilter/TestsForFilter.cs | 46 +++++++++++++++++++ .../TestsForFilter/TestsForFilter.csproj | 19 ++++++++ FilterFoldMap/TestsForFilter/Usings.cs | 1 + FilterFoldMap/TestsForFold/TestsForFold.cs | 44 ++++++++++++++++++ .../TestsForFold/TestsForFold.csproj | 19 ++++++++ FilterFoldMap/TestsForFold/Usings.cs | 1 + FilterFoldMap/TestsForMap/TestsForMap.cs | 46 +++++++++++++++++++ FilterFoldMap/TestsForMap/TestsForMap.csproj | 19 ++++++++ FilterFoldMap/TestsForMap/Usings.cs | 1 + 15 files changed, 327 insertions(+) create mode 100644 FilterFoldMap/FilterFoldMap.sln create mode 100644 FilterFoldMap/FilterFoldMap/Filter.cs create mode 100644 FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj create mode 100644 FilterFoldMap/FilterFoldMap/Fold.cs create mode 100644 FilterFoldMap/FilterFoldMap/Map.cs create mode 100644 FilterFoldMap/FilterFoldMap/Program.cs create mode 100644 FilterFoldMap/TestsForFilter/TestsForFilter.cs create mode 100644 FilterFoldMap/TestsForFilter/TestsForFilter.csproj create mode 100644 FilterFoldMap/TestsForFilter/Usings.cs create mode 100644 FilterFoldMap/TestsForFold/TestsForFold.cs create mode 100644 FilterFoldMap/TestsForFold/TestsForFold.csproj create mode 100644 FilterFoldMap/TestsForFold/Usings.cs create mode 100644 FilterFoldMap/TestsForMap/TestsForMap.cs create mode 100644 FilterFoldMap/TestsForMap/TestsForMap.csproj create mode 100644 FilterFoldMap/TestsForMap/Usings.cs diff --git a/FilterFoldMap/FilterFoldMap.sln b/FilterFoldMap/FilterFoldMap.sln new file mode 100644 index 0000000..aaea8cf --- /dev/null +++ b/FilterFoldMap/FilterFoldMap.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterFoldMap", "FilterFoldMap\FilterFoldMap.csproj", "{340904FA-0366-4E8A-B2EB-015B49CCCC73}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Debug|Any CPU.Build.0 = Debug|Any CPU + {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Release|Any CPU.ActiveCfg = Release|Any CPU + {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6777423C-FDA1-4947-AE5E-CE0822B66085} + EndGlobalSection +EndGlobal diff --git a/FilterFoldMap/FilterFoldMap/Filter.cs b/FilterFoldMap/FilterFoldMap/Filter.cs new file mode 100644 index 0000000..54fffe5 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/Filter.cs @@ -0,0 +1,31 @@ +namespace MapFilterFold; + +/// +/// Implements the filter function +/// +public class FilterList +{ + /// + /// Filters the list with the selected type using the function + /// + /// Selected data type + /// A list with the selected type + /// The function for filtering a list item returns a boolean value if the list item matches the filter + /// New list after filter + public List Filter(List list, Func function) + { + if (list == null) + { + return null; + } + List newList = new List(); + foreach (var item in list) + { + if (function(item)) + { + newList.Add(item); + } + } + return newList; + } +} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj new file mode 100644 index 0000000..f02677b --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj @@ -0,0 +1,10 @@ + + + + Exe + net7.0 + enable + enable + + + diff --git a/FilterFoldMap/FilterFoldMap/Fold.cs b/FilterFoldMap/FilterFoldMap/Fold.cs new file mode 100644 index 0000000..51afd9c --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/Fold.cs @@ -0,0 +1,28 @@ +namespace MapFilterFold; + +/// +/// Implements the fold function +/// +public class FoldList +{ + /// + /// The function accumulates the original value + /// + /// The type that the function works with + /// A list with the selected type + /// Cumulative value with the selected type + /// A function for accumulating a value, takes an accumulating value and a list item + /// Accumulated value + public TypeOfValue Fold(List list, TypeOfValue accumulatedValue, Func function) + { + if (list == null) + { + return default(TypeOfValue); + } + foreach (var item in list) + { + accumulatedValue = function(item, accumulatedValue); + } + return accumulatedValue; + } +} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/Map.cs b/FilterFoldMap/FilterFoldMap/Map.cs new file mode 100644 index 0000000..f7b7a17 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/Map.cs @@ -0,0 +1,28 @@ +namespace MapFilterFold; + +/// +/// Implements the map function +/// +public class MapList +{ + /// + /// The map changes the value of the list with the selected type according to the function + /// + /// Selected data type + /// A list with the selected data type + /// Function for conversion + /// A new list obtained using the function and the original list + public List Map(List list, Func function) + { + if (list == null) + { + return null; + } + List newList = new List(); + foreach (var item in list) + { + newList.Add(function(item)); + } + return newList; + } +} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/Program.cs b/FilterFoldMap/FilterFoldMap/Program.cs new file mode 100644 index 0000000..6afcdf5 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/Program.cs @@ -0,0 +1,9 @@ +namespace MapFilterFold; + +class Program +{ + public static void Main() + { + + } +} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFilter/TestsForFilter.cs b/FilterFoldMap/TestsForFilter/TestsForFilter.cs new file mode 100644 index 0000000..2f2f566 --- /dev/null +++ b/FilterFoldMap/TestsForFilter/TestsForFilter.cs @@ -0,0 +1,46 @@ +namespace TestsForMap; + +using MapFilterFold; + +public class Tests +{ + FilterList map; + + [SetUp] + public void Setup() + { + map = new FilterList(); + } + + [TestCaseSource(nameof(FilterForTest))] + public void TheListOnIntShouldWorkCorrectly(FilterList filter) + { + List list = new List { 1, 2, 3 }; + List listCheck = new List { 2 }; + list = filter.Filter(list, x => x % 2 == 0); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [TestCaseSource(nameof(FilterForTest))] + public void TheListOnCharShouldWorkCorrectly(FilterList filter) + { + List list = new List { '2', '3', '4' }; + List listCheck = new List { '2', '4' }; + list = filter.Filter(list, x => x % 2 == 0); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [TestCaseSource(nameof(FilterForTest))] + public void AnEmptyListShouldFinishTheJobCorrectly(FilterList filter) + { + List list = null; + list = filter.Filter(list, x => x % 2 == 0); + Assert.IsTrue(list == null); + } + + private static IEnumerable FilterForTest + => new TestCaseData[] + { + new TestCaseData(new FilterList()) + }; +} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFilter/TestsForFilter.csproj b/FilterFoldMap/TestsForFilter/TestsForFilter.csproj new file mode 100644 index 0000000..cbb7690 --- /dev/null +++ b/FilterFoldMap/TestsForFilter/TestsForFilter.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/FilterFoldMap/TestsForFilter/Usings.cs b/FilterFoldMap/TestsForFilter/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/FilterFoldMap/TestsForFilter/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/FilterFoldMap/TestsForFold/TestsForFold.cs b/FilterFoldMap/TestsForFold/TestsForFold.cs new file mode 100644 index 0000000..4a612ea --- /dev/null +++ b/FilterFoldMap/TestsForFold/TestsForFold.cs @@ -0,0 +1,44 @@ +namespace TestsForMap; + +using MapFilterFold; + +public class Tests +{ + FoldList fold; + + [SetUp] + public void Setup() + { + fold = new FoldList(); + } + + [TestCaseSource(nameof(FoldForTest))] + public void TheListOnIntShouldWorkCorrectly(FoldList fold) + { + List list = new List { 1, 2, 3 }; + var returnedNumber = fold.Fold(list, 1, (acc, elem) => acc * elem); + Assert.IsTrue(returnedNumber == 6); + } + + [TestCaseSource(nameof(FoldForTest))] + public void TheListOnCharShouldWorkCorrectly(FoldList fold) + { + List list = new List { '1', '2', '3' }; + var returnedNumber = fold.Fold(list, '1', (acc, elem) => (char)(acc * elem)); + Assert.IsTrue(returnedNumber == '氶'); + } + + [TestCaseSource(nameof(FoldForTest))] + public void AnEmptyListShouldFinishTheJobCorrectly(FoldList fold) + { + List list = null; + var returnedNumber = fold.Fold(list, 1, (acc, elem) => acc * elem); + Assert.IsTrue(list == null); + } + + private static IEnumerable FoldForTest + => new TestCaseData[] + { + new TestCaseData(new FoldList()) + }; +} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFold/TestsForFold.csproj b/FilterFoldMap/TestsForFold/TestsForFold.csproj new file mode 100644 index 0000000..cbb7690 --- /dev/null +++ b/FilterFoldMap/TestsForFold/TestsForFold.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/FilterFoldMap/TestsForFold/Usings.cs b/FilterFoldMap/TestsForFold/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/FilterFoldMap/TestsForFold/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file diff --git a/FilterFoldMap/TestsForMap/TestsForMap.cs b/FilterFoldMap/TestsForMap/TestsForMap.cs new file mode 100644 index 0000000..496453d --- /dev/null +++ b/FilterFoldMap/TestsForMap/TestsForMap.cs @@ -0,0 +1,46 @@ +namespace TestsForMap; + +using MapFilterFold; + +public class Tests +{ + MapList map; + + [SetUp] + public void Setup() + { + map = new MapList(); + } + + [TestCaseSource(nameof(MapForTest))] + public void TheListOnIntShouldWorkCorrectly(MapList map) + { + List list = new List { 1, 2, 3 }; + List listCheck = new List { 2, 4, 6 }; + list = map.Map(list, x => x * 2); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [TestCaseSource(nameof(MapForTest))] + public void TheListOnCharShouldWorkCorrectly(MapList map) + { + List list = new List { '1', '2', '3' }; + List listCheck = new List { 'b', 'd', 'f' }; + list = map.Map(list, x => (char)(x * 2)); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [TestCaseSource(nameof(MapForTest))] + public void AnEmptyListShouldFinishTheJobCorrectly(MapList map) + { + List list = null; + list = map.Map(list, x => (x * 2)); + Assert.IsTrue(list == null); + } + + private static IEnumerable MapForTest + => new TestCaseData[] + { + new TestCaseData(new MapList()) + }; +} \ No newline at end of file diff --git a/FilterFoldMap/TestsForMap/TestsForMap.csproj b/FilterFoldMap/TestsForMap/TestsForMap.csproj new file mode 100644 index 0000000..cbb7690 --- /dev/null +++ b/FilterFoldMap/TestsForMap/TestsForMap.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + diff --git a/FilterFoldMap/TestsForMap/Usings.cs b/FilterFoldMap/TestsForMap/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/FilterFoldMap/TestsForMap/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file From 7623b5ea2f6e77fcdc39ccd9248487a377ebdc5b Mon Sep 17 00:00:00 2001 From: Artem Date: Thu, 25 May 2023 23:34:23 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=A0=D0=B5=D0=B2=D1=8C=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FilterFoldMap/FilterFoldMap.sln | 8 +- FilterFoldMap/FilterFoldMap/Filter.cs | 31 -------- .../FilterFoldMap/FilterFoldMap.csproj | 2 +- FilterFoldMap/FilterFoldMap/Fold.cs | 28 ------- FilterFoldMap/FilterFoldMap/Map.cs | 28 ------- FilterFoldMap/FilterFoldMap/MapFilterFold.cs | 73 +++++++++++++++++ FilterFoldMap/FilterFoldMap/Program.cs | 9 --- .../TestsForFilter/TestsForFilter.cs | 46 ----------- .../TestsForFilter/TestsForFilter.csproj | 19 ----- FilterFoldMap/TestsForFold/TestsForFold.cs | 44 ----------- FilterFoldMap/TestsForFold/Usings.cs | 1 - FilterFoldMap/TestsForMap/TestsForMap.cs | 46 ----------- FilterFoldMap/TestsForMap/TestsForMap.csproj | 19 ----- FilterFoldMap/TestsForMap/Usings.cs | 1 - .../TestsForMapFilterFold.cs | 79 +++++++++++++++++++ .../TestsForMapFilterFold.csproj} | 4 + .../Usings.cs | 0 17 files changed, 164 insertions(+), 274 deletions(-) delete mode 100644 FilterFoldMap/FilterFoldMap/Filter.cs delete mode 100644 FilterFoldMap/FilterFoldMap/Fold.cs delete mode 100644 FilterFoldMap/FilterFoldMap/Map.cs create mode 100644 FilterFoldMap/FilterFoldMap/MapFilterFold.cs delete mode 100644 FilterFoldMap/FilterFoldMap/Program.cs delete mode 100644 FilterFoldMap/TestsForFilter/TestsForFilter.cs delete mode 100644 FilterFoldMap/TestsForFilter/TestsForFilter.csproj delete mode 100644 FilterFoldMap/TestsForFold/TestsForFold.cs delete mode 100644 FilterFoldMap/TestsForFold/Usings.cs delete mode 100644 FilterFoldMap/TestsForMap/TestsForMap.cs delete mode 100644 FilterFoldMap/TestsForMap/TestsForMap.csproj delete mode 100644 FilterFoldMap/TestsForMap/Usings.cs create mode 100644 FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.cs rename FilterFoldMap/{TestsForFold/TestsForFold.csproj => TestsForMapFilterFold/TestsForMapFilterFold.csproj} (85%) rename FilterFoldMap/{TestsForFilter => TestsForMapFilterFold}/Usings.cs (100%) diff --git a/FilterFoldMap/FilterFoldMap.sln b/FilterFoldMap/FilterFoldMap.sln index aaea8cf..9d3db31 100644 --- a/FilterFoldMap/FilterFoldMap.sln +++ b/FilterFoldMap/FilterFoldMap.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.4.33403.182 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterFoldMap", "FilterFoldMap\FilterFoldMap.csproj", "{340904FA-0366-4E8A-B2EB-015B49CCCC73}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FilterFoldMap", "FilterFoldMap\FilterFoldMap.csproj", "{340904FA-0366-4E8A-B2EB-015B49CCCC73}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestsForMapFilterFold", "TestsForMapFilterFold\TestsForMapFilterFold.csproj", "{5CB90C1B-AAB3-4ECF-B454-C1EDE5F501B0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Debug|Any CPU.Build.0 = Debug|Any CPU {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Release|Any CPU.ActiveCfg = Release|Any CPU {340904FA-0366-4E8A-B2EB-015B49CCCC73}.Release|Any CPU.Build.0 = Release|Any CPU + {5CB90C1B-AAB3-4ECF-B454-C1EDE5F501B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5CB90C1B-AAB3-4ECF-B454-C1EDE5F501B0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5CB90C1B-AAB3-4ECF-B454-C1EDE5F501B0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5CB90C1B-AAB3-4ECF-B454-C1EDE5F501B0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FilterFoldMap/FilterFoldMap/Filter.cs b/FilterFoldMap/FilterFoldMap/Filter.cs deleted file mode 100644 index 54fffe5..0000000 --- a/FilterFoldMap/FilterFoldMap/Filter.cs +++ /dev/null @@ -1,31 +0,0 @@ -namespace MapFilterFold; - -/// -/// Implements the filter function -/// -public class FilterList -{ - /// - /// Filters the list with the selected type using the function - /// - /// Selected data type - /// A list with the selected type - /// The function for filtering a list item returns a boolean value if the list item matches the filter - /// New list after filter - public List Filter(List list, Func function) - { - if (list == null) - { - return null; - } - List newList = new List(); - foreach (var item in list) - { - if (function(item)) - { - newList.Add(item); - } - } - return newList; - } -} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj index f02677b..dc2bb05 100644 --- a/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj +++ b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj @@ -1,7 +1,7 @@ - Exe + Library net7.0 enable enable diff --git a/FilterFoldMap/FilterFoldMap/Fold.cs b/FilterFoldMap/FilterFoldMap/Fold.cs deleted file mode 100644 index 51afd9c..0000000 --- a/FilterFoldMap/FilterFoldMap/Fold.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace MapFilterFold; - -/// -/// Implements the fold function -/// -public class FoldList -{ - /// - /// The function accumulates the original value - /// - /// The type that the function works with - /// A list with the selected type - /// Cumulative value with the selected type - /// A function for accumulating a value, takes an accumulating value and a list item - /// Accumulated value - public TypeOfValue Fold(List list, TypeOfValue accumulatedValue, Func function) - { - if (list == null) - { - return default(TypeOfValue); - } - foreach (var item in list) - { - accumulatedValue = function(item, accumulatedValue); - } - return accumulatedValue; - } -} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/Map.cs b/FilterFoldMap/FilterFoldMap/Map.cs deleted file mode 100644 index f7b7a17..0000000 --- a/FilterFoldMap/FilterFoldMap/Map.cs +++ /dev/null @@ -1,28 +0,0 @@ -namespace MapFilterFold; - -/// -/// Implements the map function -/// -public class MapList -{ - /// - /// The map changes the value of the list with the selected type according to the function - /// - /// Selected data type - /// A list with the selected data type - /// Function for conversion - /// A new list obtained using the function and the original list - public List Map(List list, Func function) - { - if (list == null) - { - return null; - } - List newList = new List(); - foreach (var item in list) - { - newList.Add(function(item)); - } - return newList; - } -} \ No newline at end of file diff --git a/FilterFoldMap/FilterFoldMap/MapFilterFold.cs b/FilterFoldMap/FilterFoldMap/MapFilterFold.cs new file mode 100644 index 0000000..ba257b1 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/MapFilterFold.cs @@ -0,0 +1,73 @@ +namespace MapFilterFold; + +/// +/// Class for filter, fold, map with generics +/// +public static class FilterFoldMap +{ + /// + /// The function accumulates the original value + /// + /// The type that the function works with + /// A list with the selected type + /// Cumulative value with the selected type + /// A function for accumulating a value, takes an accumulating value and a list item + /// Accumulated value + public static TypeForResult Fold(List list, TypeForResult accumulatedValue, Func function) + { + if (list == null) + { + throw new NullReferenceException(); + } + foreach (var item in list) + { + accumulatedValue = function(item, accumulatedValue); + } + return accumulatedValue; + } + + /// + /// Filters the list with the selected type using the function + /// + /// Selected data type + /// A list with the selected type + /// The function for filtering a list item returns a boolean value if the list item matches the filter + /// New list after filter + public static List Filter(List list, Func function) + { + if (list == null) + { + throw new NullReferenceException(); + } + List newList = new(); + foreach (var item in list) + { + if (function(item)) + { + newList.Add(item); + } + } + return newList; + } + + /// + /// The map changes the value of the list with the selected type according to the function + /// + /// Selected data type + /// A list with the selected data type + /// Function for conversion + /// A new list obtained using the function and the original list + public static List Map(List list, Func function) + { + if (list == null) + { + throw new NullReferenceException(); + } + List newList = new(); + foreach (var item in list) + { + newList.Add(function(item)); + } + return newList; + } +} diff --git a/FilterFoldMap/FilterFoldMap/Program.cs b/FilterFoldMap/FilterFoldMap/Program.cs deleted file mode 100644 index 6afcdf5..0000000 --- a/FilterFoldMap/FilterFoldMap/Program.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MapFilterFold; - -class Program -{ - public static void Main() - { - - } -} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFilter/TestsForFilter.cs b/FilterFoldMap/TestsForFilter/TestsForFilter.cs deleted file mode 100644 index 2f2f566..0000000 --- a/FilterFoldMap/TestsForFilter/TestsForFilter.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace TestsForMap; - -using MapFilterFold; - -public class Tests -{ - FilterList map; - - [SetUp] - public void Setup() - { - map = new FilterList(); - } - - [TestCaseSource(nameof(FilterForTest))] - public void TheListOnIntShouldWorkCorrectly(FilterList filter) - { - List list = new List { 1, 2, 3 }; - List listCheck = new List { 2 }; - list = filter.Filter(list, x => x % 2 == 0); - Assert.IsTrue(list.SequenceEqual(listCheck)); - } - - [TestCaseSource(nameof(FilterForTest))] - public void TheListOnCharShouldWorkCorrectly(FilterList filter) - { - List list = new List { '2', '3', '4' }; - List listCheck = new List { '2', '4' }; - list = filter.Filter(list, x => x % 2 == 0); - Assert.IsTrue(list.SequenceEqual(listCheck)); - } - - [TestCaseSource(nameof(FilterForTest))] - public void AnEmptyListShouldFinishTheJobCorrectly(FilterList filter) - { - List list = null; - list = filter.Filter(list, x => x % 2 == 0); - Assert.IsTrue(list == null); - } - - private static IEnumerable FilterForTest - => new TestCaseData[] - { - new TestCaseData(new FilterList()) - }; -} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFilter/TestsForFilter.csproj b/FilterFoldMap/TestsForFilter/TestsForFilter.csproj deleted file mode 100644 index cbb7690..0000000 --- a/FilterFoldMap/TestsForFilter/TestsForFilter.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - net7.0 - enable - enable - - false - - - - - - - - - - - diff --git a/FilterFoldMap/TestsForFold/TestsForFold.cs b/FilterFoldMap/TestsForFold/TestsForFold.cs deleted file mode 100644 index 4a612ea..0000000 --- a/FilterFoldMap/TestsForFold/TestsForFold.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace TestsForMap; - -using MapFilterFold; - -public class Tests -{ - FoldList fold; - - [SetUp] - public void Setup() - { - fold = new FoldList(); - } - - [TestCaseSource(nameof(FoldForTest))] - public void TheListOnIntShouldWorkCorrectly(FoldList fold) - { - List list = new List { 1, 2, 3 }; - var returnedNumber = fold.Fold(list, 1, (acc, elem) => acc * elem); - Assert.IsTrue(returnedNumber == 6); - } - - [TestCaseSource(nameof(FoldForTest))] - public void TheListOnCharShouldWorkCorrectly(FoldList fold) - { - List list = new List { '1', '2', '3' }; - var returnedNumber = fold.Fold(list, '1', (acc, elem) => (char)(acc * elem)); - Assert.IsTrue(returnedNumber == '氶'); - } - - [TestCaseSource(nameof(FoldForTest))] - public void AnEmptyListShouldFinishTheJobCorrectly(FoldList fold) - { - List list = null; - var returnedNumber = fold.Fold(list, 1, (acc, elem) => acc * elem); - Assert.IsTrue(list == null); - } - - private static IEnumerable FoldForTest - => new TestCaseData[] - { - new TestCaseData(new FoldList()) - }; -} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFold/Usings.cs b/FilterFoldMap/TestsForFold/Usings.cs deleted file mode 100644 index cefced4..0000000 --- a/FilterFoldMap/TestsForFold/Usings.cs +++ /dev/null @@ -1 +0,0 @@ -global using NUnit.Framework; \ No newline at end of file diff --git a/FilterFoldMap/TestsForMap/TestsForMap.cs b/FilterFoldMap/TestsForMap/TestsForMap.cs deleted file mode 100644 index 496453d..0000000 --- a/FilterFoldMap/TestsForMap/TestsForMap.cs +++ /dev/null @@ -1,46 +0,0 @@ -namespace TestsForMap; - -using MapFilterFold; - -public class Tests -{ - MapList map; - - [SetUp] - public void Setup() - { - map = new MapList(); - } - - [TestCaseSource(nameof(MapForTest))] - public void TheListOnIntShouldWorkCorrectly(MapList map) - { - List list = new List { 1, 2, 3 }; - List listCheck = new List { 2, 4, 6 }; - list = map.Map(list, x => x * 2); - Assert.IsTrue(list.SequenceEqual(listCheck)); - } - - [TestCaseSource(nameof(MapForTest))] - public void TheListOnCharShouldWorkCorrectly(MapList map) - { - List list = new List { '1', '2', '3' }; - List listCheck = new List { 'b', 'd', 'f' }; - list = map.Map(list, x => (char)(x * 2)); - Assert.IsTrue(list.SequenceEqual(listCheck)); - } - - [TestCaseSource(nameof(MapForTest))] - public void AnEmptyListShouldFinishTheJobCorrectly(MapList map) - { - List list = null; - list = map.Map(list, x => (x * 2)); - Assert.IsTrue(list == null); - } - - private static IEnumerable MapForTest - => new TestCaseData[] - { - new TestCaseData(new MapList()) - }; -} \ No newline at end of file diff --git a/FilterFoldMap/TestsForMap/TestsForMap.csproj b/FilterFoldMap/TestsForMap/TestsForMap.csproj deleted file mode 100644 index cbb7690..0000000 --- a/FilterFoldMap/TestsForMap/TestsForMap.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - net7.0 - enable - enable - - false - - - - - - - - - - - diff --git a/FilterFoldMap/TestsForMap/Usings.cs b/FilterFoldMap/TestsForMap/Usings.cs deleted file mode 100644 index cefced4..0000000 --- a/FilterFoldMap/TestsForMap/Usings.cs +++ /dev/null @@ -1 +0,0 @@ -global using NUnit.Framework; \ No newline at end of file diff --git a/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.cs b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.cs new file mode 100644 index 0000000..b266916 --- /dev/null +++ b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.cs @@ -0,0 +1,79 @@ +namespace TestsForMapFilterFold; + +using MapFilterFold; + +public class Tests +{ + [Test] + public void TheListOnIntShouldWorkCorrectlyWithFilter() + { + List list = new List { 1, 2, 3 }; + List listCheck = new List { 2 }; + list = FilterFoldMap.Filter(list, x => x % 2 == 0); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [Test] + public void TheListOnCharShouldWorkCorrectlyWithFilter() + { + List list = new List { '2', '3', '4' }; + List listCheck = new List { '2', '4' }; + list = FilterFoldMap.Filter(list, x => x % 2 == 0); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [Test] + public void AnEmptyListShouldFinishTheJobCorrectlyWithFilter() + { + List list = null; + Assert.Throws(() => FilterFoldMap.Filter(list, x => x % 2 == 0)); + } + + [Test] + public void TheListOnIntShouldWorkCorrectlyWithMap() + { + List list = new List { 1, 2, 3 }; + List listCheck = new List { 2, 4, 6 }; + list = FilterFoldMap.Map(list, x => x * 2); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [Test] + public void TheListOnCharShouldWorkCorrectlyWithMap() + { + List list = new List { '1', '2', '3' }; + List listCheck = new List { 'b', 'd', 'f' }; + list = FilterFoldMap.Map(list, x => (char)(x * 2)); + Assert.IsTrue(list.SequenceEqual(listCheck)); + } + + [Test] + public void AnEmptyListShouldFinishTheJobCorrectlyWithMap() + { + List list = null; + Assert.Throws(() => FilterFoldMap.Map(list, x => (x * 2))); + } + + [Test] + public void TheListOnIntShouldWorkCorrectlyWithFold() + { + List list = new List { 1, 2, 3 }; + var returnedNumber = FilterFoldMap.Fold(list, 1, (acc, elem) => acc * elem); + Assert.That(6, Is.EqualTo(returnedNumber)); + } + + [Test] + public void TheListOnCharShouldWorkCorrectlyWithFold() + { + List list = new List { '1', '2', '3' }; + var returnedNumber = FilterFoldMap.Fold(list, '1', (acc, elem) => (char)(acc * elem)); + Assert.That('氶', Is.EqualTo(returnedNumber)); + } + + [Test] + public void AnEmptyListShouldFinishTheJobCorrectlyWithFold() + { + List list = null; + Assert.Throws(() => FilterFoldMap.Fold(list, 1, (acc, elem) => acc * elem)); + } +} \ No newline at end of file diff --git a/FilterFoldMap/TestsForFold/TestsForFold.csproj b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj similarity index 85% rename from FilterFoldMap/TestsForFold/TestsForFold.csproj rename to FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj index cbb7690..2fc0404 100644 --- a/FilterFoldMap/TestsForFold/TestsForFold.csproj +++ b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/FilterFoldMap/TestsForFilter/Usings.cs b/FilterFoldMap/TestsForMapFilterFold/Usings.cs similarity index 100% rename from FilterFoldMap/TestsForFilter/Usings.cs rename to FilterFoldMap/TestsForMapFilterFold/Usings.cs