ich Typen einer Nachricht verarbeiten wollenVerarbeitet MailboxProcessor nur IObservable?
Add x
macht erinnern Programmnummer x
Print
macht es zu merkende Nummern
drucken Warum würde ich dieses schreiben:
open System
type Message =
| Add of int
| Print
let mailbox = new MailboxProcessor<Message>(fun inbox ->
let rec loop history = async{
let! msg=inbox.Receive()
match msg with
| Add x -> return! loop(history + x.ToString()+" ")
| Print ->
printfn "%s" history
return! loop(history)
}
loop ""
)
[<EntryPoint>]
let main argv =
mailbox.Start()
mailbox.Post(Add 12)
mailbox.Post(Add 56)
mailbox.Post(Print)
mailbox.Post(Add 34)
mailbox.Post(Print)
ignore <| Console.ReadLine()
0
statt davon:
open System
open System.Reactive.Subjects
type Message =
| Add of int
| Print
let subject = new Subject<Message>()
[<EntryPoint>]
let main argv =
subject
|> Observable.scan(fun history msg ->
match msg with
| Add x -> history + x.ToString()+" "
| Print ->
printfn "%s" history
history
) ""
|> Observable.subscribe(fun _->())
|> ignore
subject.OnNext(Add 12)
subject.OnNext(Add 56)
subject.OnNext(Print)
subject.OnNext(Add 34)
subject.OnNext(Print)
ignore <| Console.ReadLine()
0
Die MailboxProcessor
fügt zusätzliche Komplexität hinzu. Ich brauche eine Zustandsmaschine, die einen Zustand annimmt und einen Zustand zurückgibt. Aber es zwingt mich, inbox
zu nehmen, die verwendet wird, um Zustand zu erhalten.
Hat es irgendwelche Vorteile zu IObservable
?