Skip to content

Instantly share code, notes, and snippets.

@mythz
Created September 22, 2011 21:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
@kenegozi
Copy link

awesome, except for one thing - 45% tax in the US? that's Israel's buddy.

@mythz
Copy link
Author

mythz commented Sep 22, 2011

ahh apologies, still pretty new to the US :)

@kenegozi
Copy link

lol. I'd like to be able to get used to lower taxes. I'm still stuck with the system described above

@mythz
Copy link
Author

mythz commented Sep 22, 2011

lol, unlucky :) come on over the water - the weather's fine! (its only the economic climate that you need to worry about :-)

@flq
Copy link

flq commented Sep 23, 2011

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 |> ?

@mythz
Copy link
Author

mythz commented Sep 23, 2011

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

@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