Last active
January 19, 2022 20:27
-
-
Save HoraceGonzalez/b874d886a3b13e864f59ef8d3d44dafe to your computer and use it in GitHub Desktop.
F# Actor Example
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 | |
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