3/12/2009

What is F#? Microsoft Multi-paradigm Language CLR – All the .NET platform benefits First class language in to Visual Studio 2010

What about F# makes it functional or concurrent? Immutable Constructs Higher Order Functions Partial Application Ability to do lazy evaluation Asynchronous Workflows

1

3/12/2009

Real World Application! Auto Insurance Rating Engine

2

3/12/2009 3 .

3/12/2009 List of Policies Policy Policy Policy Calculation Calculation Calculation Driver Factor Company Car Factor Multi-policy Factor Zipcode Factor Coverage Coverage Coverage Vehicle Vehicle Driver Driver Driver Driver Why can’t it be faster? 4 .

GetCoverages(p)) } |> Async. GetCurrentRates(p).3/12/2009 Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multi-policy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let rec Rate (policies: System.of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p.Collections.Generic.Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 5 .List<Policy>) = let policies = List.

of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p.of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p.Collections.List<Policy>) = let policies = List.3/12/2009 let Rate (policies: System.Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let rec Rate (policies: System. GetCurrentRates(p).Collections. GetCoverages(p)) } |> Async.List<Policy>) = let policies = List.Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 6 . GetCoverages(p)) } |> Async.Generic.Generic. GetCurrentRates(p).

Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let rec Rate (policies: System.Generic. GetCoverages(p)) } |> Async.Generic.List<Policy>) = let policies = List.List<Policy>) = let policies = List. GetCoverages(p)) } |> Async.of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p. GetCurrentRates(p).of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p.Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 7 .Collections.Collections. GetCurrentRates(p).3/12/2009 let rec Rate (policies: System.

Run(GetRates policy rates c))) calcCoverages policy rates tail Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let rec calcCoverages (policy:Policy) rates coverages = function | [] -> () | c::tail -> updatePolicy(policy (Async.Run(GetRates policy rates c))) calcCoverages policy rates tail Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 8 .3/12/2009 let rec calcCoverages (policy:Policy) rates coverages = function | [] -> () | c::tail -> updatePolicy(policy (Async.

reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 9 .3/12/2009 let GetRates (policy:Policy) rates coverage = async{ let! results = Async. Seq. Seq.reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let GetRates (policy:Policy) rates coverage = async{ let! results = Async.vehicles -> async{return (Result policy v coverage rates).vehicles -> async{return (Result policy v coverage rates).Parallel[ for v in Policy.Force()} ] return (coverage.Parallel[ for v in Policy.Force()} ] return (coverage.

reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let GetRates (policy:Policy) rates coverage = async{ let! results = Async.Force()} ] return (coverage. Seq.vehicles -> async{return (Result policy v coverage rates).reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 10 .3/12/2009 let GetRates (policy:Policy) rates coverage = async{ let! results = Async.Parallel[ for v in Policy.vehicles -> async{return (Result policy v coverage rates).Force()} ] return (coverage. Seq.Parallel[ for v in Policy.

Parallel[ for v in Policy.Force()} ] return (coverage.Force()} ] return (coverage.vehicles -> async{return (Result policy v coverage rates). Seq.vehicles -> async{return (Result policy v coverage rates).reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let GetRates (policy:Policy) rates coverage = async{ let! results = Async.3/12/2009 let GetRates (policy:Policy) rates coverage = async{ let! results = Async.Parallel[ for v in Policy.reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 11 . Seq.

.Coverage <. BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 12 . . .c.GetRate(rq) .ToInt() r.3/12/2009 let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq. BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq.GetRate(rq) . .ToInt() r.Coverage <.c.

3/12/2009 let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq.ToInt() r.c. .GetRate(rq) .Coverage <.Coverage <.ToInt() r. . BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 13 . .GetRate(rq) . . BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq.c.

c. BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq.GetRate(rq) . . . BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 14 .ToInt() r.Coverage <.c.GetRate(rq) .ToInt() r. . .Coverage <.3/12/2009 let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq.

reduce (*) ) (Lazy<decimal>) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let BasicPolicyFactor() = lazy( Seq.] |> Seq.map force [AverageDriverFactor(). CompanyCarDiscount().3/12/2009 let BasicPolicyFactor() = lazy( Seq.] |> Seq. MultiPolicyCombinationDiscount(). MultiPolicyCombinationDiscount().map force [AverageDriverFactor(). CompanyCarDiscount().reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 15 .

reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let AverageDriverFactor() = lazy( let mutable accum = (decimal)0 for d in p.] |> Seq.Drivers.accum + (DriverFactor d).Count) (unit -> Lazy<decimal>) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 16 .map force [AverageDriverFactor(). MultiPolicyCombinationDiscount(). CompanyCarDiscount().Force() accum / (decimal)p.Drivers do accum <.3/12/2009 let BasicPolicyFactor() = lazy( Seq.

Count) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 17 .Count) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let AverageDriverFactor() = lazy( let mutable accum = (decimal)0 for d in p.Drivers do accum <.Drivers do accum <.accum + (DriverFactor d).Force() accum / (decimal)p.3/12/2009 let AverageDriverFactor() = lazy( let mutable accum = (decimal)0 for d in p.Drivers.Drivers.Force() accum / (decimal)p.accum + (DriverFactor d).

GoodStudentFactor d.map force |> Seq. DriverClassFactor d. AwayAtSchoolFactor d] |> Seq.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 18 . InsuranceScoreFactor d.3/12/2009 let DriverFactor d = lazy( [BaseRate d. GoodStudentFactor d. InsuranceScoreFactor d.map force |> Seq. GrangeLifeFactor d. DriverClassFactor d. AwayAtSchoolFactor d] |> Seq.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let DriverFactor d = lazy( [BaseRate d. GrangeLifeFactor d.

CreditScore <.Age <.InsuranceScore Process rq.CalculateDriverAge(d. InsuranceScoreFactor d.ToString() with | "CBRN" when score < 2 -> -2 | "CBRS" when score < 2 -> -3 | "CBRW" when score < 2 -> -4 | "CBRV" when score < 2 -> -5 | "CBRX" when score < 2 -> -6 | _ -> d. AwayAtSchoolFactor d] |> Seq.InsuranceScoreAttribute. p) let rec scoreCalc() = match d.3/12/2009 let DriverFactor d = lazy( [BaseRate d.map force |> Seq.Common. DriverClassFactor d.CreditScoreFactor) rq. GoodStudentFactor d. GrangeLifeFactor d.scoreCalc() Spawned Thread QueryRate rq List of Policies Policy Policy Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 19 .BirthDate.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let InsuranceScoreFactor (d:Driver) = let rq = new RateQuery(RateTableType.

CalculateDriverAge(d.InsuranceScoreAttribute.CalculateDriverAge(d.CreditScoreFactor) rq.InsuranceScore Process rq.Common.ToString() with | "CBRN" when score < 2 -> -2 | "CBRS" when score < 2 -> -3 | "CBRW" when score < 2 -> -4 | "CBRV" when score < 2 -> -5 | "CBRX" when score < 2 -> -6 | _ -> d.Common.3/12/2009 let InsuranceScoreFactor (d:Driver) = let rq = new RateQuery(RateTableType.ToString() with | "CBRN" when score < 2 -> -2 | "CBRS" when score < 2 -> -3 | "CBRW" when score < 2 -> -4 | "CBRV" when score < 2 -> -5 | "CBRX" when score < 2 -> -6 | _ -> d.Age <.BirthDate.CreditScore <. p) let rec scoreCalc() = match d.InsuranceScoreAttribute. p) let rec scoreCalc() = match d.Age <.CreditScore <.InsuranceScore Process rq.scoreCalc() Spawned Thread QueryRate rq List of Policies Policy Policy Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let InsuranceScoreFactor (d:Driver) = let rq = new RateQuery(RateTableType.BirthDate.CreditScoreFactor) rq.scoreCalc() Spawned Thread QueryRate rq List of Policies Policy Policy Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 20 .

ToString() with | "CBRN" when score < 2 -> -2 | "CBRS" when score < 2 -> -3 | "CBRW" when score < 2 -> -4 | "CBRV" when score < 2 -> -5 | "CBRX" when score < 2 -> -6 | _ -> d.Age <.scoreCalc() Spawned Thread QueryRate rq List of Policies Policy Policy Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let InsuranceScoreFactor (d:Driver) = let rq = new RateQuery(RateTableType.CreditScore <.CreditScoreFactor) rq. p) let rec scoreCalc() = match d.BirthDate. p) let rec scoreCalc() = match d.Common.Age <.BirthDate.3/12/2009 let InsuranceScoreFactor (d:Driver) = let rq = new RateQuery(RateTableType.InsuranceScore Process rq.CreditScoreFactor) rq.InsuranceScoreAttribute.CalculateDriverAge(d.InsuranceScoreAttribute.Common.CreditScore <.ToString() with | "CBRN" when score < 2 -> -2 | "CBRS" when score < 2 -> -3 | "CBRW" when score < 2 -> -4 | "CBRV" when score < 2 -> -5 | "CBRX" when score < 2 -> -6 | _ -> d.CalculateDriverAge(d.scoreCalc() Spawned Thread QueryRate rq List of Policies Policy Policy Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 21 .InsuranceScore Process rq.

GoodStudentFactor d. GoodStudentFactor d. GrangeLifeFactor d. InsuranceScoreFactor d. InsuranceScoreFactor d.map force |> Seq. GrangeLifeFactor d.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let DriverFactor d = lazy( [BaseRate d.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 22 .map force |> Seq. DriverClassFactor d. AwayAtSchoolFactor d] |> Seq. DriverClassFactor d.3/12/2009 let DriverFactor d = lazy( [BaseRate d. AwayAtSchoolFactor d] |> Seq.

AwayAtSchoolFactor d] |> Seq. DriverClassFactor d.Force() Spawned Thread Parallel Parallel Policy Policy Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let DriverFactor d = lazy( [BaseRate d.3/12/2009 let DriverFactor d = lazy( [BaseRate d. GoodStudentFactor d. GrangeLifeFactor d. DriverClassFactor d. InsuranceScoreFactor d.reduce (*) ) Process Spawned Thread Policy Policy List of Policies Policy decimal list Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 23 .map force |> Seq. InsuranceScoreFactor d.reduce (*) ) Process List of Policies let force (x:Lazy<x>) = x. GrangeLifeFactor d.map force |> Seq. GoodStudentFactor d. AwayAtSchoolFactor d] |> Seq.

DriverClassFactor d. InsuranceScoreFactor d.3/12/2009 let DriverFactor d = lazy( [BaseRate d.map force |> Seq. DriverClassFactor d.map force |> Seq. GrangeLifeFactor d. AwayAtSchoolFactor d] |> Seq. GoodStudentFactor d. GoodStudentFactor d.reduce (*) ) Process List of Policies decimal Spawned Thread Parallel Parallel Policy Policy Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 24 . GrangeLifeFactor d.reduce (*) ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let DriverFactor d = lazy( [BaseRate d. InsuranceScoreFactor d. AwayAtSchoolFactor d] |> Seq.

Count ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 25 .Force() accum / (decimal)p.Drivers.accum + (DriverFactor d).Drivers do accum <.accum + (DriverFactor d).Drivers.3/12/2009 let AverageDriverFactor() = lazy( let mutable accum = (decimal)0 for d in p.Count ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let AverageDriverFactor() = lazy( let mutable accum = (decimal)0 for d in p.Drivers do accum <.Force() accum / (decimal)p.

.GetRate(rq) .] |> Seq. . BasicPolicyFactor() ) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 26 .reduce (*) ) (unit -> Lazy<decimal>) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let Result (p:Policy)(v:Vehicle)(c:AutoCoverage)(r:CurrentRates) = lazy( let QueryRate(rq:RateQuery) = rq. CompanyCarDiscount().c.map force [AverageDriverFactor().ToInt() r.3/12/2009 let BasicPolicyFactor() = lazy( Seq. MultiPolicyCombinationDiscount().Coverage <.

Seq.3/12/2009 let GetRates (policy:Policy) rates coverage = async{ let! results = Async.vehicles -> async{return (Result policy v coverage rates).reduce(+) results)} Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor let CalcCoverages (policy:Policy) rates coverages = function | [] -> () | c::tail -> updatePolicy(policy.Run(GetRates policy rates c))) Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 27 .Force()} ] return (coverage.Parallel[ for v in Policy. (Async.

GetCoverages(p)) } |> Async.List<Policy>) = let policies = List.3/12/2009 let rec Rate (policies: System.Generic.of_seq policies let rec exec = function |[] -> () |p::tail -> async{ calcCoverages(p.Spawn exec tail exec policies Process Spawned Thread Parallel Parallel Policy Policy List of Policies Policy Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multipolicy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor Process Spawned Thread Policy Policy List of Policies Policy Parallel Parallel Coverage Coverage Coverage Coverage Coverage Vehicle Vehicle Basic Policy Factor Avg Driver Factor Company Car Factor Multi-policy Factor Zipcode Factor (d1 + d2) / 2 Driver Factor Driver Driver Driver Factor 28 . GetCurrentRates(p).Collections.

Laucher@SophicGroup.3/12/2009 1 C# F# 3 1 5 10 4 10 15 7 20 38 15 40 78 20 50 95 35 #light let RealWorld F# = Concurrent applications Amanda Laucher Amanda.net pandamonial.com Twitter: pandamonial 29 .

Sign up to vote on this title
UsefulNot useful