Skip to content

Instantly share code, notes, and snippets.

@mythz
Created September 22, 2011 21:26
Show Gist options
  • Save mythz/1236106 to your computer and use it in GitHub Desktop.
Save mythz/1236106 to your computer and use it in GitHub Desktop.
Calculate a tax rates using F#
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
@shenoyroopesh
Copy link

Ah.. this is beautiful!

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