Created
August 19, 2011 13:57
-
-
Save davidgrenier/1156849 to your computer and use it in GitHub Desktop.
Erlang Ring problem in F#
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
open System | |
type Agent<'a> = MailboxProcessor<'a> | |
type Msg = Step of int | End | |
let getRingHead n = | |
let lastNode = | |
Agent.Start(fun inbox -> | |
let rec loop (time : DateTime) = async { | |
let! m = inbox.Receive() | |
if m = End then | |
printfn "%.2fs" (DateTime.Now - time).TotalSeconds | |
return! loop time | |
} | |
loop DateTime.Now) | |
let getPrevAgent (agent : _ Agent) = | |
Agent.Start(fun inbox -> async { | |
while true do | |
let! m = inbox.Receive() | |
agent.Post m | |
}) | |
Seq.unfold (fun a -> Some(a, getPrevAgent a)) lastNode |> Seq.nth n | |
let run nodeCount messageCount = | |
let post = (getRingHead nodeCount).Post | |
Seq.init messageCount Step |> Seq.iter post | |
post End | |
run 100 100000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment