Created
November 22, 2009 21:54
-
-
Save chrisforbes/240743 to your computer and use it in GitHub Desktop.
This file contains hidden or 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; | |
namespace event_combinators | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var textChanged = new XO<string>(null); /* warning: this doesn't actually work */ | |
var q = textChanged.Select(a => a.Length); | |
var interestingEvents = q.Until(textChanged); | |
} | |
} | |
interface IO<T> { IDisposable Attach(IB<T> ib); } | |
interface IB<T> { void Yield(T t); void Break(); void Throw(Exception e); } | |
#region Anonymous Interface Implementation Helpers | |
class XO<T> : IO<T> | |
{ | |
Func<IB<T>, IDisposable> _attach; | |
public XO(Func<IB<T>, IDisposable> attach) { this._attach = attach; } | |
public IDisposable Attach(IB<T> ib) { return _attach(ib); } | |
} | |
class XB<T> : IB<T> | |
{ | |
Action<T> _yield; | |
Action _break; | |
Action<Exception> _throw; | |
public XB(Action<T> _yield, Action _break, Action<Exception> _throw) | |
{ | |
this._yield = _yield; | |
this._break = _break; | |
this._throw = _throw; | |
} | |
public void Yield(T t) { _yield(t); } | |
public void Break() { _break(); } | |
public void Throw(Exception e) { _throw(e); } | |
} | |
#endregion | |
static class Combinators | |
{ | |
public static IO<U> Select<T, U>(this IO<T> o, Func<T, U> f) | |
{ | |
return new XO<U>( | |
ib => o.Attach(new XB<T>( | |
t => ib.Yield(f(t)), | |
ib.Break, | |
ib.Throw))); | |
} | |
public static IO<T> Until<T,U>(this IO<T> o, IO<U> u) | |
{ | |
return new XO<T>( | |
ib => | |
{ | |
var d = o.Attach(ib); | |
u.Attach(new XB<U>( | |
_ => d.Dispose(), | |
() => { }, | |
ib.Throw)); | |
return d; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment