Skip to content

Instantly share code, notes, and snippets.

@ryanheath
Forked from flq/tax.cs
Created September 22, 2011 15:49
Show Gist options
  • Save ryanheath/1235117 to your computer and use it in GitHub Desktop.
Save ryanheath/1235117 to your computer and use it in GitHub Desktop.
the tax thingy
public decimal GetTaxes(decimal salary)
{
var taxBrackets = new[]
{
new {UpperLimit = 5070m, Taxation = 0.10m},
new {UpperLimit = 8660m, Taxation = 0.14m},
new {UpperLimit = 14070m, Taxation = 0.23m},
new {UpperLimit = 21240m, Taxation = 0.30m},
new {UpperLimit = 40230m, Taxation = 0.33m},
new {UpperLimit = decimal.MaxValue, Taxation = 0.45m},
};
var taxSlices = new[] {new {UpperLimit = 0m, Taxation = 0m}}
.Concat(taxBrackets)
.Zip(taxBrackets,
(previousBracket, taxBracket) => new
{
Slice = taxBracket.UpperLimit - previousBracket.UpperLimit,
taxBracket.Taxation
});
var tax = 0m;
foreach (var taxSlice in taxSlices)
{
var taxedSlice = Math.Min(taxSlice.Slice, salary);
tax += taxedSlice * taxSlice.Taxation;
salary -= taxedSlice;
}
return tax;
}
@ryanheath
Copy link
Author

I could not resist to create a version that determine its own slices :)

@ryanheath
Copy link
Author

Added some Zip love, much nicer solution now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment