Skip to content

Instantly share code, notes, and snippets.

@zahardzhan
Created October 6, 2011 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zahardzhan/1269001 to your computer and use it in GitHub Desktop.
Save zahardzhan/1269001 to your computer and use it in GitHub Desktop.
F# vs Zahardzhan's C# actors
// type Message = Finished | Msg of int * Message MailboxProcessor
// let (<--) (m:MailboxProcessor<_>) x = m.Post(x)
// let ping iters (outbox : Message MailboxProcessor) =
// MailboxProcessor.Start(fun inbox ->
// let rec loop n = async {
// match n with
// | 0 -> outbox.Post Finished
// printfn "ping finished"
// return ()
// | _ -> outbox <-- Msg(n, inbox)
// let! msg = inbox.Receive()
// printfn "ping received pong"
// return! loop(n-1)}
// loop iters)
// let pong() =
// MailboxProcessor.Start(fun inbox ->
// let rec loop () = async {
// let! msg = inbox.Receive()
// match msg with
// | Finished ->
// printfn "pong finished"
// return ()
// | Msg(n, outbox) ->
// printfn "pong received ping"
// outbox <-- Msg(n, inbox)
// return! loop() }
// loop())
// ping 100 <| pong() |> ignore
// System.Console.ReadLine() |> ignore
// ---------------------------------------------------
// ---------------------------------------------------
static class Program {
struct Finished { }
struct Msg { public int N; public Actor Sender; }
static Actor Ping(int iters, Actor pong) {
return Actor.Startup(self => {
var i = iters;
loop: if (i == 0) {
pong.Send(new Finished { });
Console.WriteLine("ping finished");
} else {
pong.Send(new Msg { N = i, Sender = self });
var msg = self.Receive<Msg>();
Console.WriteLine("ping received pong");
i--; goto loop;
}
});
}
static Actor Pong() {
return Actor.Startup(self => {
loop: var msg = self.Receive<object>();
if (msg is Finished) {
Console.WriteLine("pong finished");
return;
} else if (msg is Msg) {
var n = ((Msg)msg).N;
var sender = ((Msg)msg).Sender;
Console.WriteLine("pong received ping");
sender.Send(new Msg { N = n, Sender = self });
goto loop;
}
});
}
static void Main() {
Ping(100, Pong());
Console.ReadLine();
}
}
@zahardzhan
Copy link
Author

Хочешь нормальных акторов на C# — сделай их сам.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment