type Agent<'T> = MailboxProcessor<'T> /// Represents different messages /// handled by the stats agent type StatsMessage = | Add of float | Clear | GetAverage of AsyncReplyChannel let stats = Agent.Start(fun inbox -> // Loops, keeping a list of numbers let rec loop nums = async { let! msg = inbox.Receive() match msg with | Add num -> let newNums = num::nums return! loop newNums | GetAverage repl -> repl.Reply(List.average nums) return! loop nums | Clear -> return! loop [] } loop [] ) // Add error handler stats.Error.Add(fun e -> printfn "Oops: %A" e) // Post messages stats.Post(Add(10.0)) stats.Post(Add(7.0)) stats.Post(Clear) stats.PostAndReply(GetAverage) (* Chat room message: * NewMessage with name and text * GetAllMessages returns all messages via AsyncReplyChannel *)