Skip to content

Instantly share code, notes, and snippets.

@davidgrenier
Created August 19, 2011 13:57
Show Gist options
  • Save davidgrenier/1156849 to your computer and use it in GitHub Desktop.
Save davidgrenier/1156849 to your computer and use it in GitHub Desktop.
Erlang Ring problem in F#
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