12 people like it.
Like the snippet!
Agent demo
Agent demo
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
|
type Agent<'T> = MailboxProcessor<'T>
/// Represents different messages
/// handled by the stats agent
type StatsMessage =
| Add of float
| Clear
| GetAverage of AsyncReplyChannel<float>
let stats =
Agent.Start(fun inbox ->
// Loops, keeping a list of numbers
let rec loop nums = async {
let! msg = inbox.Receive()
match msg with
| Add num ->
let newNums = num::nums
return! loop newNums
| GetAverage repl ->
repl.Reply(List.average nums)
return! loop nums
| Clear ->
return! loop [] }
loop [] )
// Add error handler
stats.Error.Add(fun e -> printfn "Oops: %A" e)
// Post messages
stats.Post(Add(10.0))
stats.Post(Add(7.0))
stats.Post(Clear)
stats.PostAndReply(GetAverage)
(*
Chat room message:
* NewMessage with name and text
* GetAllMessages returns all messages
via AsyncReplyChannel
*)
|
Multiple items
type MailboxProcessor<'Msg> =
interface IDisposable
new : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:CancellationToken -> MailboxProcessor<'Msg>
member Post : message:'Msg -> unit
member PostAndAsyncReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> Async<'Reply>
member PostAndReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> 'Reply
member PostAndTryAsyncReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> Async<'Reply option>
member Receive : ?timeout:int -> Async<'Msg>
member Scan : scanner:('Msg -> Async<'T> option) * ?timeout:int -> Async<'T>
member Start : unit -> unit
member TryPostAndReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> 'Reply option
...
Full name: Microsoft.FSharp.Control.MailboxProcessor<_>
--------------------
new : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:System.Threading.CancellationToken -> MailboxProcessor<'Msg>
type StatsMessage =
| Add of float
| Clear
| GetAverage of AsyncReplyChannel<float>
Full name: Script.StatsMessage
Represents different messages
handled by the stats agent
union case StatsMessage.Add: float -> StatsMessage
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
union case StatsMessage.Clear: StatsMessage
union case StatsMessage.GetAverage: AsyncReplyChannel<float> -> StatsMessage
type AsyncReplyChannel<'Reply>
member Reply : value:'Reply -> unit
Full name: Microsoft.FSharp.Control.AsyncReplyChannel<_>
val stats : MailboxProcessor<StatsMessage>
Full name: Script.stats
type Agent<'T> = MailboxProcessor<'T>
Full name: Script.Agent<_>
static member MailboxProcessor.Start : body:(MailboxProcessor<'Msg> -> Async<unit>) * ?cancellationToken:System.Threading.CancellationToken -> MailboxProcessor<'Msg>
val inbox : MailboxProcessor<StatsMessage>
val loop : (float list -> Async<'a>)
val nums : float list
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
val msg : StatsMessage
member MailboxProcessor.Receive : ?timeout:int -> Async<'Msg>
val num : float
val newNums : float list
val repl : AsyncReplyChannel<float>
member AsyncReplyChannel.Reply : value:'Reply -> unit
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
val average : list:'T list -> 'T (requires member ( + ) and member DivideByInt and member get_Zero)
Full name: Microsoft.FSharp.Collections.List.average
event MailboxProcessor.Error: IEvent<Handler<System.Exception>,System.Exception>
member System.IObservable.Add : callback:('T -> unit) -> unit
val e : System.Exception
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
member MailboxProcessor.Post : message:'Msg -> unit
member MailboxProcessor.PostAndReply : buildMessage:(AsyncReplyChannel<'Reply> -> 'Msg) * ?timeout:int -> 'Reply
More information