Skip to content

Instantly share code, notes, and snippets.

@HoraceGonzalez
Last active January 19, 2022 20:27
Show Gist options
  • Save HoraceGonzalez/b874d886a3b13e864f59ef8d3d44dafe to your computer and use it in GitHub Desktop.
Save HoraceGonzalez/b874d886a3b13e864f59ef8d3d44dafe to your computer and use it in GitHub Desktop.
F# Actor Example
open System
type Message =
| Hi
| Greet of string
let greeterActor = MailboxProcessor.Start(fun inbox ->
// the message processing function
let rec messageLoop() = async{
// read a message
let! msg = inbox.Receive()
// process the message
match msg with
| Hi ->
printfn "Hi!"
| Greet msg ->
printfn $"Hello, {msg}!"
// // loop to top
return! messageLoop()
}
// start the loop
messageLoop()
)
greeterActor.Post Hi // send a Hi message to the actor's message queue
greeterActor.Post (Greet "world") // send a Greet message to the actor's message queue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment