Skip to content

Instantly share code, notes, and snippets.

@mythz mythz/taxOfUs.fs
Created Sep 22, 2011

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

kenegozi commented Sep 22, 2011

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

@mythz

This comment has been minimized.

Copy link
Owner Author

mythz commented Sep 22, 2011

ahh apologies, still pretty new to the US :)

@kenegozi

This comment has been minimized.

Copy link

kenegozi commented Sep 22, 2011

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

@mythz

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

shenoyroopesh commented Mar 30, 2012

Ah.. this is beautiful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.