Created
October 18, 2013 14:38
-
-
Save FoleyAxel/7042524 to your computer and use it in GitHub Desktop.
type mailbox
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
| type resultMsg = | |
| | Add of string | |
| | Fetch of AsyncReplyChannel<Set<string>> | |
| type resultContainer() = | |
| let innerContainer = | |
| MailboxProcessor.Start(fun inbox -> | |
| let rec loop (n:Set<string>) = | |
| async { let! msg = inbox.Receive() | |
| match msg with | |
| | Add x -> return! loop (n.Add x) | |
| | Fetch(reply) -> | |
| reply.Reply(n); | |
| return! loop n } | |
| loop Set.empty) | |
| member this.Add(x) = innerContainer.Post(Add x) | |
| member this.Fetch() = innerContainer.PostAndReply((fun reply -> Fetch(reply)), timeout = 2000) |
davidgrenier
commented
Oct 18, 2013
type resultMsg =
| Add of string
| Fetch of AsyncReplyChannel<Set<string>>
type Cache =
{
Add: string -> 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)
| Fetch reply ->
reply.Reply data
return! loop data
}
loop Set.empty
)
{
Add = Add >> mb.Post
Fetch = fun() -> mb.PostAndReply Fetch
}
cache.Add "3"
cache.Fetch()type resultMsg =
| Add of string
| Fetch of AsyncReplyChannel<Set<string>>
type ICache =
abstract Add: string -> unit
abstract 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)
| Fetch reply ->
reply.Reply data
return! loop data
}
loop Set.empty
)
{
new ICache with
member __.Add x = Add x |> mb.Post
member __.Fetch() = mb.PostAndReply Fetch
}
cache.Add "3"
cache.Fetch()type resultMsg =
| Add of string
| Fetch of AsyncReplyChannel<Set<string>>
type Cache =
{
Add: string -> unit
Fetch: unit -> Set<string>
}
let cache =
let mb =
MailboxProcessor.Start(fun inbox ->
let data = ref Set.empty
async {
while true do
let! msg = inbox.Receive()
match msg with
| Add x ->
data:= Set.add x !data
| Fetch reply ->
reply.Reply !data
}
)
{
Add = Add >> mb.Post
Fetch = fun() -> mb.PostAndReply Fetch
}
cache.Add "3"
cache.Fetch()
Author
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 "3"
cache.Fetch()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment