Created
March 22, 2013 20:42
-
-
Save ldematte/5224579 to your computer and use it in GitHub Desktop.
Testing async (continuation) monads in C#
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Linq.Expressions; | |
namespace Dematte.Tests | |
{ | |
class Program | |
{ | |
// let! a = in1.Receive //first 2 | |
// let a = a + 1 | |
// let! b = in2.Receive | |
// let! _ = out.Send(a + b) | |
// return Unit.One | |
//cml.Bind(in1.Receive, (fun a -> | |
// cml.Let(a + 1, (fun a -> | |
// cml.Bind(in2.Receive, (fun b -> | |
// cml.Bind(out.Send(a + b), fun _ -> | |
// cml.Return(Unit.One)))))))) | |
public static void Test() | |
{ | |
var in1 = new Channel<int>(); | |
var in2 = new Channel<int>(); | |
var out1 = new Channel<int>(); | |
var comp = new BindContinuables<int, int>(in1.Receive, (a) => | |
new LetContinuables<int, int>(a + 1, (b) => | |
new BindContinuables<int, int>(in2.Receive, (c) => | |
new BindContinuables<Unit, int>(out1.Send(b + c), (d) => | |
new UnitContinuable<int>())))); | |
comp.ContinueWith((int u) => { }); | |
} | |
public static Task Spawn(Action<Action<Unit>> computation) | |
{ | |
var t1 = new Task(() => computation((Unit u) => { })); | |
t1.Start(); | |
return t1; | |
} | |
public static Task Spawn<T>(Action<Action<T>> c, Action<T> k) | |
{ | |
var t = new Task(() => c(k)); | |
t.Start(); | |
return t; | |
} | |
public static Unit WriteLine(string s) | |
{ | |
Console.WriteLine(s); | |
return Unit.One; | |
} | |
public static Unit Do<T>(Action<T> f, T arg) | |
{ | |
f(arg); | |
return Unit.One; | |
} | |
public static Unit Do(Action f) | |
{ | |
f(); | |
return Unit.One; | |
} | |
public static void Test2() | |
{ | |
var in1 = new Channel<int>(); | |
var in2 = new Channel<int>(); | |
var out1 = new Channel<int>(); | |
Action<Action<Unit>> thread1 = null; | |
thread1 = from a in in1.Receive.Invoke() //let! | |
let b = a + 1 | |
// | |
let _x = WriteLine("Received " + b) | |
from c in in2.Receive.Invoke() | |
let _x1 = Do(Console.WriteLine, "Got " + c) | |
let _x2 = Do(() => Console.WriteLine("Got " + c)) | |
from x in out1.Send(b + c).Invoke() | |
from z in thread1 | |
select z; //return | |
//select thread1; | |
var thread2 = from x1 in in1.Send(3).Invoke() | |
from x2 in in2.Send(3).Invoke() | |
from res in out1.Receive.Invoke() | |
select res; | |
var t1 = Spawn(thread1); | |
var t2 = Spawn(thread2, u => | |
{ | |
Console.WriteLine("Result is: " + u); | |
}); | |
Task.WaitAll(t1, t2); | |
//let! i = inCh.Receive in | |
// if ((i % p) <> 0) | |
// then let! _ = outCh.Send(i) in return! loop() | |
// else return! loop() | |
} | |
static void Main(string[] args) | |
{ | |
Test2(); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment