diff --git a/FilterFoldMap/FilterFoldMap.sln b/FilterFoldMap/FilterFoldMap.sln new file mode 100644 index 0000000..9d3db31 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33403.182 +MinimumVisualStudioVersion = 10.0.40219.1 +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 + 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 + {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 + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6777423C-FDA1-4947-AE5E-CE0822B66085} + EndGlobalSection +EndGlobal diff --git a/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj new file mode 100644 index 0000000..dc2bb05 --- /dev/null +++ b/FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj @@ -0,0 +1,10 @@ + + + + Library + net7.0 + enable + enable + + + 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/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/TestsForMapFilterFold/TestsForMapFilterFold.csproj b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj new file mode 100644 index 0000000..2fc0404 --- /dev/null +++ b/FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/FilterFoldMap/TestsForMapFilterFold/Usings.cs b/FilterFoldMap/TestsForMapFilterFold/Usings.cs new file mode 100644 index 0000000..cefced4 --- /dev/null +++ b/FilterFoldMap/TestsForMapFilterFold/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; \ No newline at end of file