Created
August 14, 2015 12:54
-
-
Save lepoetemaudit/668164c05c0faa725eac to your computer and use it in GitHub Desktop.
F# mailboxes
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
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