Created
September 22, 2011 10:08
-
-
Save flq/1234461 to your computer and use it in GitHub Desktop.
the tax thingy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
var progression = 0; is not very good, since you could also use "int" instead of "var" and be more explicit at the same cost.
Cool!
Whoa, that is one slick implementation.
Here's a functional programming stylee version! https://gist.github.com/1235276
Also a F# example with composition using currying! https://gist.github.com/1236106
yeah, looks unbeatable
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can't beat that snippet