Created
January 7, 2014 19:48
-
-
Save digitalBush/8305621 to your computer and use it in GitHub Desktop.
Port of Bryan Hunter's (@bryan_hunter) Cheap Process Erlang demo to F# (https://github.com/bryanhunter/IntroToErlangTalk/blob/nashjug-2013/cheap_process/cheap_process.erl)
This file contains hidden or 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
module CheapProcess = | |
open System.Diagnostics | |
type Agent<'T> = MailboxProcessor<'T> | |
type Answer = | |
Ok | |
let rec start howMany (pid: Agent<Answer>) = | |
Agent.Start(fun inbox -> | |
async { | |
match howMany with | |
| 0 -> | |
pid.Post(Ok) | |
return () | |
| _ -> | |
let child = start (howMany-1) pid | |
child.Post(Ok) | |
let! msg = inbox.Receive() | |
match msg with | |
| Ok -> return () | |
}) | |
let startAndTime howMany = | |
Agent.Start(fun inbox -> | |
async { | |
let clock = Stopwatch.StartNew() | |
let child=start howMany inbox | |
let! msg=inbox.Receive() | |
printfn "%O" clock.Elapsed | |
return () | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment