Created
September 22, 2011 21:26
-
-
Save mythz/1236106 to your computer and use it in GitHub Desktop.
Calculate a tax rates using F#
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
let taxOf salary taxRates = | |
((0m,0)::taxRates, taxRates) | |
||> Seq.zip | |
|> Seq.map(fun ((_, prevBand),(rate, band)) -> (prevBand, rate, band)) | |
|> Seq.sumBy(fun (prevBand, rate, band) -> | |
match salary with | |
| x when x < prevBand -> 0m | |
| x when x > band -> decimal(band - prevBand) * rate | |
| x -> decimal(x - prevBand) * rate | |
) | |
//define custom tax bands and rates | |
let israelTaxRates = [ | |
0.10m, 5070; | |
0.14m, 8660; | |
0.23m, 14070; | |
0.30m, 21240; | |
0.33m, 40230; | |
0.45m, System.Int32.MaxValue] | |
//use currying to build a higher order function to calculate Israel Tax Rates | |
let taxOfIsrael salary = israelTaxRates |> taxOf salary | |
//taxOfIsrael 5000 = 500.00 | |
//taxOfIsrael 5800 = 609.20 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah.. this is beautiful!