Skip to content

Instantly share code, notes, and snippets.

@Szer
Last active August 17, 2017 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/fe82ff2f861f59170f34ab1ff966d334 to your computer and use it in GitHub Desktop.
Save Szer/fe82ff2f861f59170f34ab1ff966d334 to your computer and use it in GitHub Desktop.
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