Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions MapFilterFold/MapFilterFold.Src/Functions.cs
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А зачем делать nullable?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не максимально общие типы, input и output типы могут различаться

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
Copy link

Choose a reason for hiding this comment

The 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;
}
}
9 changes: 9 additions & 0 deletions MapFilterFold/MapFilterFold.Src/MapFilterFold.csproj
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>
65 changes: 65 additions & 0 deletions MapFilterFold/MapFilterFold.Tests/MapFilterFold.Test.cs
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 MapFilterFold/MapFilterFold.Tests/MapFilterFold.Tests.csproj
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>
28 changes: 28 additions & 0 deletions MapFilterFold/MapFilterFold.sln
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