Skip to content

Instantly share code, notes, and snippets.

@lepoetemaudit
Created August 14, 2015 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepoetemaudit/668164c05c0faa725eac to your computer and use it in GitHub Desktop.
Save lepoetemaudit/668164c05c0faa725eac to your computer and use it in GitHub Desktop.
F# mailboxes
open System.Threading
type ChatMessage =
| SendMessage of string
| GetContent of AsyncReplyChannel<string list>
let agent = MailboxProcessor.Start(fun agent ->
let rec loop (state : string list) = async {
printfn "List length is now %d" state.Length
let! msg = agent.Receive()
match msg with
| SendMessage text ->
return! loop (text :: state)
| GetContent reply ->
reply.Reply state
return! loop state
}
loop []
)
for i in [1..10] do
Thread.Sleep 500
SendMessage (sprintf "Dave %d" i) |> agent.Post
let reply = agent.PostAndReply GetContent
reply |> List.rev |> String.concat ", " |> printfn "Messages: %s"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment