Instantly share code, notes, and snippets.

@Szer /MapReduce.fsx
Last active Aug 17, 2017

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