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
31 changes: 31 additions & 0 deletions FilterFoldMap/FilterFoldMap.sln
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 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
10 changes: 10 additions & 0 deletions FilterFoldMap/FilterFoldMap/FilterFoldMap.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
73 changes: 73 additions & 0 deletions FilterFoldMap/FilterFoldMap/MapFilterFold.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
namespace MapFilterFold;

/// <summary>
/// Class for filter, fold, map with generics
/// </summary>
public static class FilterFoldMap
{
/// <summary>
/// The function accumulates the original value
/// </summary>
/// <typeparam name="TypeOfValue">The type that the function works with</typeparam>
/// <param name="list">A list with the selected type</param>
/// <param name="accumulatedValue">Cumulative value with the selected type</param>
/// <param name="function">A function for accumulating a value, takes an accumulating value and a list item</param>
/// <returns>Accumulated value</returns>
public static TypeForResult Fold<TypeForResult, TypeOfValue>(List<TypeOfValue> list, TypeForResult accumulatedValue, Func<TypeOfValue, TypeForResult, TypeForResult> function)
{
if (list == null)
{
throw new NullReferenceException();
}
foreach (var item in list)
{
accumulatedValue = function(item, accumulatedValue);
}
return accumulatedValue;
}

/// <summary>
/// Filters the list with the selected type using the function
/// </summary>
/// <typeparam name="TypeOfValue">Selected data type</typeparam>
/// <param name="list">A list with the selected type</param>
/// <param name="function">The function for filtering a list item returns a boolean value if the list item matches the filter</param>
/// <returns>New list after filter</returns>
public static List<TypeOfValue> Filter<TypeOfValue>(List<TypeOfValue> list, Func<TypeOfValue, bool> function)
{
if (list == null)
{
throw new NullReferenceException();
}
List<TypeOfValue> newList = new();
foreach (var item in list)
{
if (function(item))
{
newList.Add(item);
}
}
return newList;
}

/// <summary>
/// The map changes the value of the list with the selected type according to the function
/// </summary>
/// <typeparam name="TypeOfValue">Selected data type</typeparam>
/// <param name="list">A list with the selected data type</param>
/// <param name="function">Function for conversion</param>
/// <returns>A new list obtained using the function and the original list</returns>
public static List<TypeForResult> Map<TypeForResult, TypeOfValue>(List<TypeOfValue> list, Func<TypeOfValue, TypeForResult> function)
{
if (list == null)
{
throw new NullReferenceException();
}
List<TypeForResult> newList = new();
foreach (var item in list)
{
newList.Add(function(item));
}
return newList;
}
}
79 changes: 79 additions & 0 deletions FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
namespace TestsForMapFilterFold;

using MapFilterFold;

public class Tests
{
[Test]
public void TheListOnIntShouldWorkCorrectlyWithFilter()
{
List<int> list = new List<int> { 1, 2, 3 };
List<int> listCheck = new List<int> { 2 };
list = FilterFoldMap.Filter(list, x => x % 2 == 0);
Assert.IsTrue(list.SequenceEqual(listCheck));
}

[Test]
public void TheListOnCharShouldWorkCorrectlyWithFilter()
{
List<char> list = new List<char> { '2', '3', '4' };
List<char> listCheck = new List<char> { '2', '4' };
list = FilterFoldMap.Filter(list, x => x % 2 == 0);
Assert.IsTrue(list.SequenceEqual(listCheck));
}

[Test]
public void AnEmptyListShouldFinishTheJobCorrectlyWithFilter()
{
List<int> list = null;
Assert.Throws<NullReferenceException>(() => FilterFoldMap.Filter(list, x => x % 2 == 0));
}

[Test]
public void TheListOnIntShouldWorkCorrectlyWithMap()
{
List<int> list = new List<int> { 1, 2, 3 };
List<int> listCheck = new List<int> { 2, 4, 6 };
list = FilterFoldMap.Map(list, x => x * 2);
Assert.IsTrue(list.SequenceEqual(listCheck));
}

[Test]
public void TheListOnCharShouldWorkCorrectlyWithMap()
{
List<char> list = new List<char> { '1', '2', '3' };
List<char> listCheck = new List<char> { 'b', 'd', 'f' };
list = FilterFoldMap.Map(list, x => (char)(x * 2));
Assert.IsTrue(list.SequenceEqual(listCheck));
}

[Test]
public void AnEmptyListShouldFinishTheJobCorrectlyWithMap()
{
List<int> list = null;
Assert.Throws<NullReferenceException>(() => FilterFoldMap.Map(list, x => (x * 2)));
}

[Test]
public void TheListOnIntShouldWorkCorrectlyWithFold()
{
List<int> list = new List<int> { 1, 2, 3 };
var returnedNumber = FilterFoldMap.Fold(list, 1, (acc, elem) => acc * elem);
Assert.That(6, Is.EqualTo(returnedNumber));
}

[Test]
public void TheListOnCharShouldWorkCorrectlyWithFold()
{
List<char> list = new List<char> { '1', '2', '3' };
var returnedNumber = FilterFoldMap.Fold(list, '1', (acc, elem) => (char)(acc * elem));
Assert.That('氶', Is.EqualTo(returnedNumber));
}

[Test]
public void AnEmptyListShouldFinishTheJobCorrectlyWithFold()
{
List<int> list = null;
Assert.Throws<NullReferenceException>(() => FilterFoldMap.Fold(list, 1, (acc, elem) => acc * elem));
}
}
23 changes: 23 additions & 0 deletions FilterFoldMap/TestsForMapFilterFold/TestsForMapFilterFold.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FilterFoldMap\FilterFoldMap.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions FilterFoldMap/TestsForMapFilterFold/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;