Created
February 23, 2014 20:16
-
-
Save andybrackley/9176636 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
(* Agents *) | |
type CounterMessage = | |
| Update of float | |
| Reset | |
let inbox = MailboxProcessor.Start(fun agent -> | |
// Function that implements the body of the agent | |
let rec loop sum count = async { | |
// Asynchronously wait for the next message | |
let! msg = agent.Receive() | |
match msg with | |
| Reset -> | |
// Restart loop with initial values | |
return! loop 0.0 0.0 | |
| Update value -> | |
// Update the state and print the statistics | |
let sum, count = sum + value, count + 1.0 | |
printfn "Average: %f" (sum / count) | |
// Wait before handling the next message | |
do! Async.Sleep(1000) | |
return! loop sum count | |
} | |
// Start the body with initial values | |
loop 0.0 0.0) | |
inbox.Post (Update 10.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment