Skip to content

Instantly share code, notes, and snippets.

@FoleyAxel
Created October 18, 2013 14:38
Show Gist options
  • Select an option

  • Save FoleyAxel/7042524 to your computer and use it in GitHub Desktop.

Select an option

Save FoleyAxel/7042524 to your computer and use it in GitHub Desktop.
type mailbox
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
Copy link

type resultMsg =
    | Add of string
    | Fetch of AsyncReplyChannel<Set<string>>

let add, fetch =
    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 >> mb.Post, fun() -> mb.PostAndReply Fetch

add "3"
fetch()

@davidgrenier
Copy link

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()

@davidgrenier
Copy link

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()

@davidgrenier
Copy link

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()

@FoleyAxel
Copy link
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