From bf32ebeed35756d2f047faf047c9311f587303c8 Mon Sep 17 00:00:00 2001 From: kamendov-maxim Date: Mon, 8 Apr 2024 14:54:43 +0300 Subject: [PATCH] MapFilterFold --- MapFilterFold/MapFilterFold.Src/Functions.cs | 46 +++++++++++++ .../MapFilterFold.Src/MapFilterFold.csproj | 9 +++ .../MapFilterFold.Tests/MapFilterFold.Test.cs | 65 +++++++++++++++++++ .../MapFilterFold.Tests.csproj | 28 ++++++++ MapFilterFold/MapFilterFold.sln | 28 ++++++++ 5 files changed, 176 insertions(+) create mode 100644 MapFilterFold/MapFilterFold.Src/Functions.cs create mode 100644 MapFilterFold/MapFilterFold.Src/MapFilterFold.csproj create mode 100644 MapFilterFold/MapFilterFold.Tests/MapFilterFold.Test.cs create mode 100644 MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj create mode 100644 MapFilterFold/MapFilterFold.sln diff --git a/MapFilterFold/MapFilterFold.Src/Functions.cs b/MapFilterFold/MapFilterFold.Src/Functions.cs new file mode 100644 index 0000000..854abd7 --- /dev/null +++ b/MapFilterFold/MapFilterFold.Src/Functions.cs @@ -0,0 +1,46 @@ +using System.Security.Cryptography; +using System.Xml.XPath; + +namespace MyFunctions; + +public class MapFilterFold +{ + public static List Map(List? input, Func? Function) + { + ArgumentNullException.ThrowIfNull(input); + ArgumentNullException.ThrowIfNull(Function); + var result = new List(); + foreach (var item in input) + { + result.Add(Function(item)); + } + return result; + } + + public static List Filter(List? input, Func? Filter) + { + ArgumentNullException.ThrowIfNull(input); + ArgumentNullException.ThrowIfNull(Filter); + var result = new List(); + foreach (var item in input) + { + if (Filter(item)) + { + result.Add(item); + } + } + return result; + } + + public static T Fold(List? input, T startValue, Func? AccumulateValue) + { + ArgumentNullException.ThrowIfNull(input); + ArgumentNullException.ThrowIfNull(AccumulateValue); + T result = startValue; + foreach (var item in input) + { + result = AccumulateValue(result, item); + } + return result; + } +} diff --git a/MapFilterFold/MapFilterFold.Src/MapFilterFold.csproj b/MapFilterFold/MapFilterFold.Src/MapFilterFold.csproj new file mode 100644 index 0000000..bb23fb7 --- /dev/null +++ b/MapFilterFold/MapFilterFold.Src/MapFilterFold.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Test.cs b/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Test.cs new file mode 100644 index 0000000..9c450ee --- /dev/null +++ b/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Test.cs @@ -0,0 +1,65 @@ +using System.ComponentModel; +using System.Security.Cryptography.X509Certificates; + +namespace MapFilterFold.Tests; + +using MyFunctions; + +public class Tests +{ + private static readonly List? intInput = [23, 543, 654, 1, 0, 5]; + private static readonly List stringInput = ["abcdefg", "qwerty", "jbdfg;abf"]; + + [Test] + public void MapTest() + { + List intAnswers = [1, 1, 0, 1, 0, 1]; + var intResult = MapFilterFold.Map(intInput, x => x % 2); + Assert.That(intResult, Is.EqualTo(intAnswers)); + + List stringsAnswers = ["aceg", "qet", "jdgaf"]; + var stringResult = MapFilterFold.Map(stringInput, RemoveEven); + Assert.That(stringResult, Is.EqualTo(stringsAnswers)); + } + + [Test] + public void FilterTest() + { + List intAnswers = [654, 0]; + var intResult = MapFilterFold.Filter(intInput, x => x % 2 == 0); + Assert.That(intResult, Is.EqualTo(intAnswers)); + + List stringsAnswers = ["qwerty"]; + var stringResult = MapFilterFold.Filter(stringInput, x => x.Length == 6); + Assert.That(stringResult, Is.EqualTo(stringsAnswers)); + } + + [Test] + public void FoldTest() + { + int intAnswer = 1226; + var intResult = MapFilterFold.Fold(intInput, 0, (x, y) => x + y); + Assert.That(intResult, Is.EqualTo(intAnswer)); + + string stringAnswer = "aqj"; + var stringResult = MapFilterFold.Fold(stringInput, "", BuildString); + Assert.That(stringResult, Is.EqualTo(stringAnswer)); + } + + static string BuildString(string startValue, string currentValue) => startValue + currentValue[0]; + + static string RemoveEven(string input) + { + var array = new char[input.Length / 2 + (input.Length % 2 == 0 ? 0 : 1)]; + int c = 0; + for (int i = 0; i < input.Length; ++i) + { + if (i % 2 == 0) + { + array[c] = input[i]; + c += 1; + } + } + return new string(array); + } +} diff --git a/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj b/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj new file mode 100644 index 0000000..b3af5b4 --- /dev/null +++ b/MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/MapFilterFold/MapFilterFold.sln b/MapFilterFold/MapFilterFold.sln new file mode 100644 index 0000000..845ffe2 --- /dev/null +++ b/MapFilterFold/MapFilterFold.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapFilterFold", "MapFilterFold.Src\MapFilterFold.csproj", "{78056446-2F9C-4600-B17D-BD6D8C6586ED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MapFilterFold.Tests", "MapFilterFold.Tests\MapFilterFold.Tests.csproj", "{69C94E85-2EA5-443F-A384-FCAA9134820B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {78056446-2F9C-4600-B17D-BD6D8C6586ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {78056446-2F9C-4600-B17D-BD6D8C6586ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {78056446-2F9C-4600-B17D-BD6D8C6586ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {78056446-2F9C-4600-B17D-BD6D8C6586ED}.Release|Any CPU.Build.0 = Release|Any CPU + {69C94E85-2EA5-443F-A384-FCAA9134820B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {69C94E85-2EA5-443F-A384-FCAA9134820B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {69C94E85-2EA5-443F-A384-FCAA9134820B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {69C94E85-2EA5-443F-A384-FCAA9134820B}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal