Skip to content

Instantly share code, notes, and snippets.

@flq
Created September 22, 2011 10:08
Show Gist options
  • Save flq/1234461 to your computer and use it in GitHub Desktop.
Save flq/1234461 to your computer and use it in GitHub Desktop.
the tax thingy
public decimal GetTaxes(decimal salary)
{
decimal tax = 0;
var progressiveTaxation = new[] { 0.1m, 0.14m, 0.23m, 0.3m, 0.33m, 0.45m };
var progressionSlices = new[] { 5070 - 0, 8660 - 5070, 14070 - 8660, 21240 - 14070, 40230 - 21240, decimal.MaxValue };
var progression = 0;
while (salary > 0)
{
var taxedSlice = salary - progressionSlices[progression] > 0 ? progressionSlices[progression] : salary;
tax += taxedSlice * progressiveTaxation[progression];
salary -= taxedSlice;
progression++;
}
return tax;
}
@daniellangnet
Copy link

var progression = 0; is not very good, since you could also use "int" instead of "var" and be more explicit at the same cost.

@dlidstrom
Copy link

Cool!

@sdgough
Copy link

sdgough commented Sep 22, 2011

Whoa, that is one slick implementation.

@NoelKennedy
Copy link

Here's a functional programming stylee version! https://gist.github.com/1235276

@mythz
Copy link

mythz commented Sep 22, 2011

Also a F# example with composition using currying! https://gist.github.com/1236106

@dornatsky
Copy link

yeah, looks unbeatable

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