-
Notifications
You must be signed in to change notification settings - Fork 0
MapFilterFold #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kamendov-maxim
wants to merge
1
commit into
master
Choose a base branch
from
MapFilterFold
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System.Security.Cryptography; | ||
| using System.Xml.XPath; | ||
|
|
||
| namespace MyFunctions; | ||
|
|
||
| public class MapFilterFold | ||
| { | ||
| public static List<T> Map<T>(List<T>? input, Func<T, T>? Function) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(input); | ||
| ArgumentNullException.ThrowIfNull(Function); | ||
| var result = new List<T>(); | ||
| foreach (var item in input) | ||
| { | ||
| result.Add(Function(item)); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static List<T> Filter<T>(List<T>? input, Func<T, bool>? Filter) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(input); | ||
| ArgumentNullException.ThrowIfNull(Filter); | ||
|
Comment on lines
+22
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. аналогично |
||
| var result = new List<T>(); | ||
| foreach (var item in input) | ||
| { | ||
| if (Filter(item)) | ||
| { | ||
| result.Add(item); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public static T Fold<T>(List<T>? input, T startValue, Func<T, T, T>? AccumulateValue) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(input); | ||
| ArgumentNullException.ThrowIfNull(AccumulateValue); | ||
| T result = startValue; | ||
| foreach (var item in input) | ||
| { | ||
| result = AccumulateValue(result, item); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using System.ComponentModel; | ||
| using System.Security.Cryptography.X509Certificates; | ||
|
|
||
| namespace MapFilterFold.Tests; | ||
|
|
||
| using MyFunctions; | ||
|
|
||
| public class Tests | ||
| { | ||
| private static readonly List<int>? intInput = [23, 543, 654, 1, 0, 5]; | ||
| private static readonly List<string> stringInput = ["abcdefg", "qwerty", "jbdfg;abf"]; | ||
|
|
||
| [Test] | ||
| public void MapTest() | ||
| { | ||
| List<int> intAnswers = [1, 1, 0, 1, 0, 1]; | ||
| var intResult = MapFilterFold.Map<int>(intInput, x => x % 2); | ||
| Assert.That(intResult, Is.EqualTo(intAnswers)); | ||
|
|
||
| List<string> stringsAnswers = ["aceg", "qet", "jdgaf"]; | ||
| var stringResult = MapFilterFold.Map(stringInput, RemoveEven); | ||
| Assert.That(stringResult, Is.EqualTo(stringsAnswers)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FilterTest() | ||
| { | ||
| List<int> intAnswers = [654, 0]; | ||
| var intResult = MapFilterFold.Filter<int>(intInput, x => x % 2 == 0); | ||
| Assert.That(intResult, Is.EqualTo(intAnswers)); | ||
|
|
||
| List<string> 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<int>(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); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
| <PackageReference Include="NUnit" Version="3.14.0" /> | ||
| <PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="NUnit.Framework" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\MapFilterFold.Src\MapFilterFold.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А зачем делать nullable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Не максимально общие типы, input и output типы могут различаться