-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
global using Xunit; |
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 | ||
{ | ||
public class ProgressiveTests | ||
{ | ||
[Fact] | ||
public void Test() | ||
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 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() | ||
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 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); | ||
} | ||
} | ||
} |
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 |
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> |
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."); | ||
} | ||
} |
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.
@ArturKarbone beauty of generic math and Primitives interoperability.