-
Notifications
You must be signed in to change notification settings - Fork 0
hw6MapFilterFold #11
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
MikePuzanov
wants to merge
2
commits into
master
Choose a base branch
from
hw6MapFilterFold
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
hw6MapFilterFold #11
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
60 changes: 60 additions & 0 deletions
60
hw6MapFilterFold/hw6MapFilterFold.Test/MapFilterFoldTest.cs
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,60 @@ | ||
| using NUnit.Framework; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace hw6MapFilterFold.Test | ||
| { | ||
| public class Tests | ||
| { | ||
| private List<int> listInt; | ||
|
|
||
| private List<string> listString; | ||
|
|
||
| private List<char> listChar; | ||
|
|
||
| [SetUp] | ||
| public void Setup() | ||
| { | ||
| listInt = new List<int>() { 1, 2, 3 }; | ||
| listString = new List<string>() { "a", "b", "c" }; | ||
| listChar = new List<char>() { 'a', 'b', 'c' }; | ||
| } | ||
|
|
||
| [Test] | ||
| public void MapTest() | ||
| { | ||
| var resultList1 = new List<int>() { 2, 4, 6 }; | ||
| var list1 = Functions.Map(listInt, x => x * 2); | ||
| Assert.AreEqual(resultList1, list1); | ||
| var resultList2 = new List<string>() { "aaaa", "baaa", "caaa" }; | ||
| var list2 = Functions.Map(listString, x => x + "aaa"); | ||
| Assert.AreEqual(resultList2, list2); | ||
| var resultList3 = new List<char>() { 'd', 'd', 'd' }; | ||
| var list3 = Functions.Map(listChar, x => 'd'); | ||
| Assert.AreEqual(resultList3, list3); | ||
| } | ||
|
|
||
| [Test] | ||
| public void FilterTest() | ||
| { | ||
| var resultList1 = new List<int>() { 2 }; | ||
| var list1 = Functions.Filter(listInt, y => y % 2 == 0 ); | ||
| Assert.AreEqual(resultList1, list1); | ||
| var resultList2 = new List<string>() { "b" }; | ||
| var list2 = Functions.Filter(listString, x => x == "b"); | ||
| Assert.AreEqual(resultList2, list2); | ||
| var resultList3 = new List<char>() { }; | ||
| 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); | ||
| } | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
hw6MapFilterFold/hw6MapFilterFold.Test/hw6MapFilterFold.Test.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,19 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
|
|
||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="NUnit" Version="3.12.0" /> | ||
| <PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\hw6MapFilterFold\hw6MapFilterFold.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,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 |
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; | ||||||
| using System.Collections.Generic; | ||||||
| using System.Linq; | ||||||
| using System.Text; | ||||||
| using System.Threading.Tasks; | ||||||
|
|
||||||
| namespace hw6MapFilterFold | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// класс с функциями для списка | ||||||
| /// </summary> | ||||||
| public static class Functions | ||||||
| { | ||||||
| /// <summary> | ||||||
| /// Возвращается список, полученный применением переданной функции к каждому элементу переданного списка | ||||||
| /// </summary> | ||||||
| /// <param name="list">список</param> | ||||||
| /// <param name="function">переданная функция</param> | ||||||
| /// <returns>новый список</returns> | ||||||
| public static List<TResult> Map<TValue, TResult>(List<TValue> list, Func<TValue, TResult> function) | ||||||
| { | ||||||
| var resultList = new List<TResult>(); | ||||||
| foreach (var node in list) | ||||||
| { | ||||||
| resultList.Add(function(node)); | ||||||
| } | ||||||
| return resultList; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// Возвращается список из элементов переданного списка, для которых переданная функция вернула true | ||||||
| /// </summary> | ||||||
| /// <param name="list">список</param> | ||||||
| /// <param name="function">булевая функция</param> | ||||||
| /// <returns>новый список</returns> | ||||||
| public static List<TValue> Filter<TValue>(List<TValue> list, Func<TValue, bool> function) | ||||||
| { | ||||||
| var returnList = new List<TValue>(); | ||||||
| foreach (var node in list) | ||||||
| { | ||||||
| if (function(node)) | ||||||
| { | ||||||
| returnList.Add(node); | ||||||
| } | ||||||
| } | ||||||
| return returnList; | ||||||
| } | ||||||
|
|
||||||
| /// <summary> | ||||||
| /// возвращает накопленное значение, получившееся после всего прохода списка | ||||||
| /// </summary> | ||||||
| /// <param name="list">список</param> | ||||||
| /// <param name="startValue">начальное значение</param> | ||||||
| /// <param name="function">функция</param> | ||||||
| /// <returns>накопленное значение</returns> | ||||||
| public static TResult Fold<TValue, TResult>(List<TValue> list, TResult startValue,Func<TResult, TValue, TResult> function) | ||||||
|
Collaborator
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.
Suggested change
|
||||||
| { | ||||||
| foreach (var node in list) | ||||||
| { | ||||||
| startValue = function(startValue, node); | ||||||
| } | ||||||
| return startValue; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
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,24 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace hw6MapFilterFold | ||
| { | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| var listInt = new List<int>() { 1, 2, 3 }; | ||
| var resultList1 = new List<int>() { 2, 4, 6 }; | ||
| var list1 = Functions.Map(listInt, x => x * 2); | ||
|
|
||
| var listString = new List<string>() { "a", "b", "c" }; | ||
| var resultList2 = new List<string>() { "aaaa", "baaa", "caaa" }; | ||
| var list2 = Functions.Map(listString, x => x + "aaa"); | ||
|
|
||
| var listChar = new List<char>() { 'a', 'b', 'c' }; | ||
| var resultList3 = new List<char>() { 'd', 'd', 'd' }; | ||
| var list3 = Functions.Map(listChar, x => 'd'); | ||
|
|
||
| } | ||
| } | ||
| } |
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,8 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net5.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
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.