Skip to content

Instantly share code, notes, and snippets.

@games
Last active October 7, 2016 10:25
Show Gist options
  • Select an option

  • Save games/2027c90a8c65396fbc5260882e348136 to your computer and use it in GitHub Desktop.

Select an option

Save games/2027c90a8c65396fbc5260882e348136 to your computer and use it in GitHub Desktop.
let (>>=) x f = Option.map f x
let (>>*) x f = Option.bind f x
let third (_, _, c) = c
type Region = Europe | USA | Japan | Others
type Car = { name: string; region: Region; price: decimal; capacity: float }
let taxRateTable = [Europe, [(0.0, 2.0, 1.0m);
(2.0, 5.0, 1.2m);
(5.0, infinity, 2.0m)];
USA, [(0.0, 2.0, 0.75m);
(2.0, 5.0, 0.9m);
(5.0, infinity, 1.5m)];
Japan, [(0.0, 2.0, 0.7m);
(2.0, 5.0, 0.8m);
(5.0, infinity, 1.35m)]]|> Map.ofList;
let inRange car (min, max, _) = car.capacity > min && car.capacity <= max
let findTaxRate car taxRates = taxRates |> List.tryFind (inRange car) >>= third
let taxRate car = taxRateTable |> Map.tryFind car.region >>* findTaxRate car
let importTax originalPrice taxRate = originalPrice * taxRate
let VAT originalPrice importTax = (originalPrice + importTax) * 0.12m, importTax
let endUserPrice originalPrice (vat, importTax) = originalPrice + importTax + vat
let calculate car = car |> taxRate >>= importTax car.price >>= VAT car.price >>= endUserPrice car.price
let USD2Pesos x = x * 47m
[<EntryPoint>]
let main argv =
let benz = { name = "Benz G65"; region = Europe; price = 217900m; capacity = 6.0 }
let hondaJazz = { name = "Honda Jazz"; region = Japan; price = 19490m; capacity = 1.5 }
let jeepWrangler = { name = "Jeep wrangler"; region = USA; price = 36995m; capacity = 3.6 }
let cheryQQ = { name = "Chery QQ"; region = Others; price = 6000m; capacity = 1.0 }
[ benz; hondaJazz; jeepWrangler; cheryQQ ]
|> List.map (fun car -> (car, calculate car))
|> List.iter (fun (x, y) ->
match y with
| Some price -> printfn "%s, %M" x.name (USD2Pesos price)
| _ -> printfn "Region %A is not support yet" x.region)
System.Console.Read() |> ignore
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment