Skip to content
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

Initial Proof of Concept #1

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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 src/Business.Calculations.Tests/Business.Calculations.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<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="Fluent.Calculations.Primitives" Version="1.1.0-aplha.39" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Business.Calculations\Business.Calculations.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions src/Business.Calculations.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using Xunit;
37 changes: 37 additions & 0 deletions src/Business.Calculations.Tests/ProgressiveTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Business.Calculations;
using Fluent.Calculations.Primitives.BaseTypes;
using FluentAssertions;

namespace Fluent.Calculations.Business.Tests
Copy link
Member Author

Choose a reason for hiding this comment

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

@ArturKarbone beauty of generic math and Primitives interoperability.

{
public class ProgressiveTests
{
[Fact]
public void Test()

Choose a reason for hiding this comment

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

Suggested name: Should_calculate_progressive_for_decimals @edgars-pivovarenoks

{
var progressive = new Progressive<decimal, decimal>()
.UpTo(100).MultiplyBy(0.01m)
.UpTo(200).MultiplyBy(0.02m)
.UpTo(300).MultiplyBy(0.03m)
.ReminderMultiplier(0.04m);

var result = progressive.Calculate(400);

result.Amount.Should().Be(10);
}

[Fact]
public void Test2()

Choose a reason for hiding this comment

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

Suggested name: Should_calculate_progressive_for_numbers @edgars-pivovarenoks

{
var progressive = new Progressive<Number, Number>()
.UpTo(100).MultiplyBy(0.01m)
.UpTo(200).MultiplyBy(0.02m)
.UpTo(300).MultiplyBy(0.03m)
.Capped();

var result = progressive.Calculate(400);

result.Amount.Primitive.Should().Be(6);
}
}
}
31 changes: 31 additions & 0 deletions src/Business.Calculations.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.8.34309.116
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Business.Calculations", "Business.Calculations\Business.Calculations.csproj", "{B6624F0A-69B5-4D2D-AA86-3C392B45DBBE}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Business.Calculations.Tests", "Business.Calculations.Tests\Business.Calculations.Tests.csproj", "{1D0EF362-7EEA-4926-A42F-1EF39F97977B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B6624F0A-69B5-4D2D-AA86-3C392B45DBBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6624F0A-69B5-4D2D-AA86-3C392B45DBBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6624F0A-69B5-4D2D-AA86-3C392B45DBBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6624F0A-69B5-4D2D-AA86-3C392B45DBBE}.Release|Any CPU.Build.0 = Release|Any CPU
{1D0EF362-7EEA-4926-A42F-1EF39F97977B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D0EF362-7EEA-4926-A42F-1EF39F97977B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D0EF362-7EEA-4926-A42F-1EF39F97977B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D0EF362-7EEA-4926-A42F-1EF39F97977B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BBD91840-5837-4742-BDE6-82CDDA0B903B}
EndGlobalSection
EndGlobal
9 changes: 9 additions & 0 deletions src/Business.Calculations/Business.Calculations.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>disable</Nullable>
</PropertyGroup>

</Project>
211 changes: 211 additions & 0 deletions src/Business.Calculations/ProgressiveGeneric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
using System.Numerics;

public class Progressive<T, TMultiplier>
where T :
ISubtractionOperators<T, T, T>,
IAdditionOperators<T, T, T>,
IMultiplyOperators<T, TMultiplier, T>,
IComparisonOperators<T, T, bool>,
new()
where TMultiplier : IMultiplyOperators<TMultiplier, T, T>
{
private readonly List<Bracket> brackets = new List<Bracket>();

private static T Zero = new T();

public record Bracket
{
public T From { get; set; }

public T To { get; set; }

public TMultiplier Multiplier { get; set; }

public bool ToInfinity { get; internal set; }

internal T Size => To - From;

public T Calculate(T value) => value * Multiplier;
}

public RateBuilder UpTo(T toValue) => new BracketRateBuilder(brackets).UpTo(Enforce.GreaterThanZero(toValue));

public ToBuilder From(T fromValue) => new BracketRateBuilder(brackets).From(Enforce.GreaterThanOrZero(fromValue));

public Progressive<T, TMultiplier> AddBracket(T fromValue, T toValue, TMultiplier multiplier)
{
brackets.Add(new Bracket { From = fromValue, To = toValue, Multiplier = multiplier });
return this;
}

public class BracketRateBuilder
{
private List<Bracket> brackets;

public BracketRateBuilder(List<Bracket> brackets) => this.brackets = brackets;

public ToBuilder From(T fromValue) => new ToBuilder(brackets, fromValue);

public RateBuilder UpTo(T toValue) => From(Zero).UpTo(toValue);
}

public class FromBuilder
{
private List<Bracket> brackets;

public FromBuilder(List<Bracket> brackets) => this.brackets = brackets;

public ToBuilder From(T fromValue) => new ToBuilder(brackets, fromValue);

public RateBuilder UpTo(T toValue) => From(brackets.Last().To).UpTo(toValue);

public ResultBuilder ReminderMultiplier(TMultiplier multipier)
{
From(brackets.Last().To).UpToInfinity().MultiplyBy(multipier);

return new ResultBuilder(brackets);
}

public ResultBuilder Capped() => new ResultBuilder(brackets);
}

public class ResultBuilder
{
private List<Bracket> brackets;

public ResultBuilder(List<Bracket> brackets) => this.brackets = brackets;

public BracketCalculationResult Calculate(T value)
{
BracketsAccumulator seed = new()
{
Remaining = value
};

return brackets.Aggregate(seed, Allocate, Calculate);

static BracketsAccumulator Allocate(BracketsAccumulator accumulator, Bracket currentBracket)
{
T allocationAmmount = accumulator.Remaining < currentBracket.Size || currentBracket.ToInfinity ?
accumulator.Remaining : currentBracket.Size;

accumulator.BracketsAllocation.Add(new BracketsAccumulator.BracketAllocation
{
Bracket = currentBracket,
Amount = allocationAmmount
});

accumulator.Remaining -= allocationAmmount;

return accumulator;
}

static BracketCalculationResult Calculate(BracketsAccumulator accumulator)
{
var breakdown = accumulator.BracketsAllocation
.Select(allocation => new BracketResult
{
Amount = allocation.Bracket.Calculate(allocation.Amount),
Bracket = allocation.Bracket
}).ToArray();

return new BracketCalculationResult
{
Amount = SumAmounts(breakdown),
Breakdown = breakdown
};

static T SumAmounts(BracketResult[] values)
{
T aggregate = Zero;

foreach (BracketResult value in values)
aggregate += value.Amount;

return aggregate;
}
}
}
}

public class BracketsAccumulator
{
public T Remaining;

public List<BracketAllocation> BracketsAllocation = new();

public class BracketAllocation
{
public Bracket Bracket { get; set; }
public T Amount { get; set; }
}
}

public class BracketCalculationResult
{
public T Amount { get; set; }

public BracketResult[] Breakdown { get; set; }
}

public class BracketResult
{
public Bracket Bracket { get; set; }

public T Amount { get; set; }
}

public class ToBuilder
{
private readonly T fromValue;
private readonly List<Bracket> brackets;

public ToBuilder(List<Bracket> brackets, T fromValue)
{
this.fromValue = fromValue;
this.brackets = brackets;
}

internal RateBuilder UpTo(T toValue) => new RateBuilder(brackets, fromValue, toValue, false);

internal RateBuilder UpToInfinity() => new RateBuilder(brackets, fromValue, new T(), true);
}

public class RateBuilder
{
private readonly T fromValue;
private readonly T toValue;
private readonly List<Bracket> brackets;
private readonly bool toInfinity;

public RateBuilder(List<Bracket> brackets, T fromValue, T toValue, bool toInfinity)
{
this.fromValue = fromValue;
this.toValue = toValue;
this.toInfinity = toInfinity;
this.brackets = brackets;
}

public FromBuilder MultiplyBy(TMultiplier multiplier)
{
brackets.Add(new Bracket
{
From = fromValue,
To = toValue,
Multiplier = multiplier,
ToInfinity = toInfinity
});

return new FromBuilder(brackets);
}
}

private class Enforce
{
internal static T GreaterThanOrZero(T fromValue) => fromValue >= Zero ? fromValue :
throw new ArgumentOutOfRangeException(nameof(fromValue), "Value shoul be greater than or equal to 0.");

internal static T GreaterThanZero(T toValue) => toValue > Zero ? toValue :
throw new ArgumentOutOfRangeException(nameof(toValue), "Value shoul be greater than 0.");
}
}