Last active
December 26, 2015 00:19
-
-
Save FoleyAxel/7063687 to your computer and use it in GitHub Desktop.
mailbox clear
This file contains hidden or 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 resultMsg = | |
| | Add of string | |
| | Fetch of AsyncReplyChannel<Set<string>> | |
| | Clear | |
| type Cache = | |
| { | |
| Add: string -> unit | |
| Clear: unit | |
| Fetch: unit -> Set<string> | |
| } | |
| let cache = | |
| let mb = | |
| MailboxProcessor.Start(fun inbox -> | |
| let rec loop data = | |
| async { | |
| let! msg = inbox.Receive() | |
| match msg with | |
| | Add x -> return! loop (Set.add x data) | |
| | Clear -> return! loop Set.empty | |
| | Fetch reply -> | |
| reply.Reply data | |
| return! loop data | |
| } | |
| loop Set.empty | |
| ) | |
| { | |
| Add = Add >> mb.Post | |
| Clear = mb.Post Clear | |
| Fetch = fun() -> mb.PostAndReply Fetch | |
| } | |
| cache.Add "1" | |
| cache.Add "2" | |
| cache.Add "3" | |
| cache.Fetch() | |
| |> Seq.iter Console.WriteLine | |
| cache.Clear | |
| cache.Add "3" | |
| cache.Add "2" | |
| cache.Add "1" | |
| cache.Fetch() | |
| |> Seq.iter Console.WriteLine |
Author
FoleyAxel
commented
Oct 20, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment