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 | |
ahh apologies, still pretty new to the US :)
lol. I'd like to be able to get used to lower taxes. I'm still stuck with the system described above
lol, unlucky :) come on over the water - the weather's fine! (its only the economic climate that you need to worry about :-)
i see the curry! that one is nice, always like a round of code golf :) really need to carve some time for F# looks pretty good for certain things. what is the difference between ||> and |> ?
yeah just learning F# now myself, really liking it so far. |> pipelines one argument to the function while ||> pipelines two arguments. i.e. all these are equivalent:
Seq.zip seq1 seq2
seq2 |> Seq.zip seq1
(seq1, seq2) ||> Seq.zip
Ah.. this is beautiful!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome, except for one thing - 45% tax in the US? that's Israel's buddy.