Last active
August 17, 2017 13:46
-
-
Save Szer/fe82ff2f861f59170f34ab1ff966d334 to your computer and use it in GitHub Desktop.
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 genericMapReduce toOutput folder src = | |
src | |
|> Seq.map toOutput | |
|> Seq.reduce folder | |
let inline mapReduceAdd toOutput src = | |
src |> genericMapReduce toOutput (+) | |
type Person = | |
{ Name : string | |
Salary : decimal } | |
type PersonReport = | |
{ Names : string list | |
SalarySum : decimal } | |
member x.AvgSalary = | |
let cachedSum = x.SalarySum / decimal x.Names.Length | |
fun () -> cachedSum | |
static member (+) (a, b) = | |
{ Names = a.Names @ b.Names | |
SalarySum = a.SalarySum + b.SalarySum } | |
let toReport person = | |
{ Names = [person.Name] | |
SalarySum = person.Salary } | |
let names = ["John";"Pitt";"Mike"] | |
let persons = | |
names | |
|> Seq.mapi (fun i n -> | |
{ Name = n; Salary = decimal (i * 100) }) | |
let report = | |
persons | |
|> mapReduceAdd toReport | |
report.AvgSalary() |> printfn "%A" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment