public void TestSwitchCSimultaneous() { Sc2 sc1 = new Sc2(0); DiscreteCellSink <Sc2> csc = DiscreteCell.CreateSink(sc1); DiscreteCell <int> co = csc.Map <DiscreteCell <int> >(b => b.C).SwitchC(); List <int> @out = new List <int>(); IListener l = co.Listen(@out.Add); Sc2 sc2 = new Sc2(3); Sc2 sc3 = new Sc2(4); Sc2 sc4 = new Sc2(7); sc1.C.Send(1); sc1.C.Send(2); csc.Send(sc2); sc1.C.Send(3); sc2.C.Send(4); sc3.C.Send(5); csc.Send(sc3); sc3.C.Send(6); sc3.C.Send(7); Transaction.RunVoid(() => { sc3.C.Send(2); csc.Send(sc4); sc4.C.Send(8); }); sc4.C.Send(9); l.Unlisten(); CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void TestLoopCell() { DiscreteCellSink <int> ca = DiscreteCell.CreateSink(22); ValueTuple <DiscreteCellLoop <int>, DiscreteCell <int>, DiscreteCell <int> > c = Transaction.Run(() => { DiscreteCellLoop <int> cbLocal = DiscreteCell.CreateLoop <int>(); DiscreteCell <int> ccLocal = ca.Map(x => x % 10).Lift(cbLocal, (x, y) => x * y); DiscreteCell <int> cbOut = ca.Map(x => x / 10); cbLocal.Loop(cbOut); return(ValueTuple.Create(cbLocal, cbOut, ccLocal)); }); DiscreteCellLoop <int> cb = c.Item1; DiscreteCell <int> cb2 = c.Item2; DiscreteCell <int> cc = c.Item3; List <int> @out = new List <int>(); List <int> out2 = new List <int>(); List <int> out3 = new List <int>(); IListener l = cb.Listen(@out.Add); IListener l2 = cb2.Listen(out2.Add); IListener l3 = cc.Listen(out3.Add); ca.Send(2); ca.Send(52); l3.Unlisten(); l2.Unlisten(); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 0, 5 }, @out.ToArray()); CollectionAssert.AreEqual(new[] { 2, 0, 5 }, out2.ToArray()); CollectionAssert.AreEqual(new[] { 4, 0, 10 }, out3.ToArray()); }
public void SwitchCOnCellLoop() { ValueTuple <DiscreteCell <int>, DiscreteCellSink <int>, DiscreteCellSink <int>, DiscreteCellSink <DiscreteCell <int> > > t = Transaction.Run(() => { DiscreteCellLoop <DiscreteCell <int> > loop = DiscreteCell.CreateLoop <DiscreteCell <int> >(); DiscreteCellSink <int> c1 = DiscreteCell.CreateSink(1); DiscreteCellSink <int> c2 = DiscreteCell.CreateSink(11); DiscreteCell <int> c = loop.SwitchC(); DiscreteCellSink <DiscreteCell <int> > s = DiscreteCell.CreateSink(c1.AsDiscreteCell()); loop.Loop(s); return(ValueTuple.Create(c, c1, c2, s)); }); List <int> output = new List <int>(); IListener l = t.Item1.Listen(output.Add); t.Item2.Send(2); t.Item3.Send(12); Transaction.RunVoid(() => { t.Item2.Send(3); t.Item3.Send(13); t.Item4.Send(t.Item3); }); t.Item2.Send(4); t.Item3.Send(14); l.Unlisten(); CollectionAssert.AreEqual(new[] { 1, 2, 13, 14 }, output); }
public void TestMap() { DiscreteCellSink <int> c = DiscreteCell.CreateSink(6); List <string> @out = new List <string>(); IListener l = c.Map(x => x.ToString()).Listen(@out.Add); c.Send(8); l.Unlisten(); CollectionAssert.AreEqual(new[] { "6", "8" }, @out); }
public void TestListen() { DiscreteCellSink <int> c = DiscreteCell.CreateSink(9); List <int> @out = new List <int>(); IListener l = c.Listen(@out.Add); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out); }
public void TestDiscreteCellValuesThenOnce() { DiscreteCellSink <int> c = DiscreteCell.CreateSink(9); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => c.Values.Once().Listen(@out.Add)); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 9 }, @out); }
public void TestApply() { DiscreteCellSink <Func <long, string> > cf = DiscreteCell.CreateSink <Func <long, string> >(x => "1 " + x); DiscreteCellSink <long> ca = DiscreteCell.CreateSink(5L); List <string> @out = new List <string>(); IListener l = ca.Apply(cf).Listen(@out.Add); cf.Send(x => "12 " + x); ca.Send(6L); l.Unlisten(); CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out); }
public void TestLift() { DiscreteCellSink <int> c1 = DiscreteCell.CreateSink(1); DiscreteCellSink <long> c2 = DiscreteCell.CreateSink(5L); List <string> @out = new List <string>(); IListener l = c1.Lift(c2, (x, y) => x + " " + y).Listen(@out.Add); c1.Send(12); c2.Send(6L); l.Unlisten(); CollectionAssert.AreEqual(new[] { "1 5", "12 5", "12 6" }, @out); }
public void TestLiftGlitch() { DiscreteCellSink <int> c1 = DiscreteCell.CreateSink(1); DiscreteCell <int> c3 = c1.Map(x => x * 3); DiscreteCell <int> c5 = c1.Map(x => x * 5); DiscreteCell <string> c = c3.Lift(c5, (x, y) => x + " " + y); List <string> @out = new List <string>(); IListener l = c.Listen(@out.Add); c1.Send(2); l.Unlisten(); CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestLiftSimultaneousUpdates() { List <int> @out = new List <int>(); DiscreteCellSink <int> cellSink = DiscreteCell.CreateSink(1); DiscreteCell <int> cell = cellSink.Map(v => 2 * v); IListener l = cellSink.Lift(cell, (x, y) => x + y).Updates.Listen(@out.Add); cellSink.Send(2); cellSink.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 6, 21 }, @out); }
public void TestDiscreteCellValuesThenLateListen() { DiscreteCellSink <int> c = DiscreteCell.CreateSink(9); List <int> @out = new List <int>(); Stream <int> value = c.Values; c.Send(8); IListener l = value.Listen(@out.Add); c.Send(2); c.Send(7); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 7 }, @out); }
public void TestCalm2() { DiscreteCellSink <int> c = DiscreteCell.CreateSink(2); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => c.Calm().Listen(@out.Add)); c.Send(4); c.Send(2); c.Send(4); c.Send(4); c.Send(2); c.Send(2); l.Unlisten(); CollectionAssert.AreEqual(new[] { 2, 4, 2, 4, 2 }, @out); }
public void TestLiftFromSimultaneous() { ValueTuple <DiscreteCellSink <int>, DiscreteCellSink <int> > t = Transaction.Run(() => { DiscreteCellSink <int> localC1 = DiscreteCell.CreateSink(3); DiscreteCellSink <int> localC2 = DiscreteCell.CreateSink(5); localC2.Send(7); return(ValueTuple.Create(localC1, localC2)); }); DiscreteCellSink <int> c1 = t.Item1; DiscreteCellSink <int> c2 = t.Item2; List <int> @out = new List <int>(); IListener l = c1.Lift(c2, (x, y) => x + y).Listen(@out.Add); l.Unlisten(); CollectionAssert.AreEqual(new[] { 10 }, @out); }
public void TestDiscreteCellValuesThenMerge() { DiscreteCellSink <int> c1 = DiscreteCell.CreateSink(9); DiscreteCellSink <int> c2 = DiscreteCell.CreateSink(2); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => c1.Values.Merge(c2.Values, (x, y) => x + y).Listen(@out.Add)); c1.Send(1); c2.Send(4); Transaction.RunVoid(() => { c1.Send(7); c2.Send(5); }); l.Unlisten(); CollectionAssert.AreEqual(new[] { 11, 1, 4, 12 }, @out); }
public void TestLiftInSwitchC() { IReadOnlyList <Test> list1 = new[] { new Test(0), new Test(1), new Test(2), new Test(3), new Test(4) }; IReadOnlyList <Test> list2 = new[] { new Test(5), new Test(6), new Test(7), new Test(8), new Test(9) }; DiscreteCellSink <IReadOnlyList <Test> > v = DiscreteCell.CreateSink(list1); DiscreteCell <IReadOnlyList <int> > c = v.Map(oo => oo.Select(o => o.Value).Lift()).SwitchC(); List <IReadOnlyList <int> > streamOutput = new List <IReadOnlyList <int> >(); IListener l = c.Updates.Listen(streamOutput.Add); List <IReadOnlyList <int> > cellOutput = new List <IReadOnlyList <int> >(); IListener l2 = c.Listen(cellOutput.Add); list1[2].Value.Send(12); list2[1].Value.Send(16); list1[4].Value.Send(14); Transaction.RunVoid(() => { list2[2].Value.Send(17); list1[0].Value.Send(10); v.Send(list2); }); list1[3].Value.Send(13); list2[3].Value.Send(18); l2.Unlisten(); l.Unlisten(); Assert.AreEqual(4, streamOutput.Count); Assert.AreEqual(5, cellOutput.Count); CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4 }, cellOutput[0]); CollectionAssert.AreEqual(new[] { 0, 1, 12, 3, 4 }, streamOutput[0]); CollectionAssert.AreEqual(new[] { 0, 1, 12, 3, 4 }, cellOutput[1]); CollectionAssert.AreEqual(new[] { 0, 1, 12, 3, 14 }, streamOutput[1]); CollectionAssert.AreEqual(new[] { 0, 1, 12, 3, 14 }, cellOutput[2]); CollectionAssert.AreEqual(new[] { 5, 16, 17, 8, 9 }, streamOutput[2]); CollectionAssert.AreEqual(new[] { 5, 16, 17, 8, 9 }, cellOutput[3]); CollectionAssert.AreEqual(new[] { 5, 16, 17, 18, 9 }, streamOutput[3]); CollectionAssert.AreEqual(new[] { 5, 16, 17, 18, 9 }, cellOutput[4]); }
public async Task TestListenAsync() { DiscreteCellSink <int> a = DiscreteCell.CreateSink(1); DiscreteCell <int> a1 = a.Map(x => x + 1); DiscreteCell <int> a2 = a.Map(x => x * 2); ValueTuple <List <int>, DiscreteCellLoop <int>, IListener> resultsAndCalled = Transaction.Run(() => { DiscreteCell <int> result = a1.Lift(a2, (x, y) => x + y); Stream <Unit> incrementStream = Operational.Value(result.Cell).MapTo(Unit.Value); StreamSink <Unit> decrementStream = Stream.CreateSink <Unit>(); DiscreteCellLoop <int> calledLoop = DiscreteCell.CreateLoop <int>(); calledLoop.Loop(incrementStream.MapTo(1).Merge(decrementStream.MapTo(-1), (x, y) => x + y).Snapshot(calledLoop.Cell, (u, c) => c + u).Hold(0)); List <int> r = new List <int>(); IListener l = result.Listen(v => { Task.Run(async() => { await Task.Delay(900); r.Add(v); decrementStream.Send(Unit.Value); }); }); return(ValueTuple.Create(r, calledLoop, l)); }); // ReSharper disable once UnusedVariable List <int> results = resultsAndCalled.Item1; DiscreteCell <int> called = resultsAndCalled.Item2; List <int> calledResults = new List <int>(); IListener l2 = called.Listen(calledResults.Add); await Task.Delay(500); a.Send(2); await Task.Delay(500); a.Send(3); await Task.Delay(2500); l2.Unlisten(); resultsAndCalled.Item3.Unlisten(); }
public void SwitchCCatchFirst() { List <int> output = new List <int>(); ValueTuple <DiscreteCell <int>, DiscreteCellSink <int>, DiscreteCellSink <int>, DiscreteCellSink <DiscreteCell <int> >, IListener> t = Transaction.Run(() => { DiscreteCellSink <int> c1 = DiscreteCell.CreateSink(1); DiscreteCellSink <int> c2 = DiscreteCell.CreateSink(11); DiscreteCellSink <DiscreteCell <int> > s = DiscreteCell.CreateSink(c1.AsDiscreteCell()); DiscreteCell <int> c = s.SwitchC(); c1.Send(2); c2.Send(12); s.Send(c2); IListener l = c.Listen(output.Add); return(ValueTuple.Create(c, c1, c2, s, l)); }); t.Item2.Send(3); t.Item3.Send(13); Transaction.RunVoid(() => { t.Item2.Send(4); t.Item3.Send(14); t.Item4.Send(t.Item2); }); t.Item2.Send(5); t.Item3.Send(15); t.Item5.Unlisten(); CollectionAssert.AreEqual(new[] { 12, 13, 4, 5 }, output); }
public void TestLiftListChangesWhileListening() { IReadOnlyList <DiscreteCellSink <int> > cellSinks = Enumerable.Range(0, 50).Select(_ => DiscreteCell.CreateSink(1)).ToArray(); DiscreteCell <int> sum = cellSinks.Lift().Map(v => v.Sum()); List <int> @out = new List <int>(); IListener l = Transaction.Run(() => { cellSinks[4].Send(5); IListener lLocal = sum.Listen(@out.Add); cellSinks[5].Send(5); return(lLocal); }); cellSinks[9].Send(5); Transaction.RunVoid(() => { cellSinks[17].Send(5); cellSinks[41].Send(5); cellSinks[48].Send(5); }); l.Unlisten(); CollectionAssert.AreEqual(new[] { 58, 62, 74 }, @out); }
public void TestLiftListLargeManyUpdates() { IReadOnlyList <DiscreteCellSink <int> > cellSinks = Enumerable.Range(0, 500).Select(_ => DiscreteCell.CreateSink(1)).ToArray(); DiscreteCell <int> sum = cellSinks.Lift().Map(v => v.Sum()); List <int> @out = new List <int>(); IListener l = sum.Listen(@out.Add); for (int i = 0; i < 100; i++) { int n = i; cellSinks[n * 5].Send(5); cellSinks[n * 5 + 1].Send(5); Transaction.RunVoid(() => { cellSinks[n * 5 + 2].Send(5); cellSinks[n * 5 + 3].Send(5); cellSinks[n * 5 + 4].Send(5); }); } l.Unlisten(); IReadOnlyList <int> expected = new[] { 500 }.Concat(Enumerable.Range(0, 100).SelectMany(n => new[] { 500 + 20 * n + 4, 500 + 20 * n + 8, 500 + 20 * n + 20 })).ToArray(); CollectionAssert.AreEqual(expected, @out); }
public void TestLiftLoopList() { ValueTuple <DiscreteCell <int>, IReadOnlyList <DiscreteCellSink <int> > > t = Transaction.Run(() => { IReadOnlyList <DiscreteCellLoop <int> > cellLoops = Enumerable.Range(0, 50).Select(_ => DiscreteCell.CreateLoop <int>()).ToArray(); DiscreteCell <int> sum = cellLoops.Lift().Map(v => v.Sum()); IReadOnlyList <DiscreteCellSink <int> > cellSinks = Enumerable.Range(0, 50).Select(_ => DiscreteCell.CreateSink(1)).ToArray(); for (int i = 0; i < 50; i++) { cellLoops[i].Loop(cellSinks[i]); } return(ValueTuple.Create(sum, cellSinks)); }); List <int> @out = new List <int>(); IListener l = t.Item1.Listen(@out.Add); t.Item2[4].Send(5); t.Item2[5].Send(5); Transaction.RunVoid(() => { t.Item2[9].Send(5); t.Item2[17].Send(5); t.Item2[41].Send(5); t.Item2[48].Send(5); }); l.Unlisten(); CollectionAssert.AreEqual(new[] { 50, 54, 58, 74 }, @out); }
public Sc2(int initialValue) { this.C = DiscreteCell.CreateSink(initialValue); }
public static void Main(string[] args) { Console.WriteLine("Press any key"); Console.ReadKey(); var(toggleAllSelectedStream, objectsAndIsSelected, selectAllStream, objects) = Transaction.Run(() => { CellLoop <bool?> allSelectedCellLoop = Cell.CreateLoop <bool?>(); StreamSink <Unit> toggleAllSelectedStreamLocal = Stream.CreateSink <Unit>(); Stream <bool> selectAllStreamLocal = toggleAllSelectedStreamLocal.Snapshot(allSelectedCellLoop).Map(a => a != true); IReadOnlyList <TestObject> o2 = Enumerable.Range(0, 10000).Select(n => new TestObject(n, selectAllStreamLocal)).ToArray(); DiscreteCellSink <IReadOnlyList <TestObject> > objectsLocal = DiscreteCell.CreateSink((IReadOnlyList <TestObject>) new TestObject[0]); var objectsAndIsSelectedLocal = objectsLocal.Map(oo => oo.Select(o => o.IsSelected.Map(s => new { Object = o, IsSelected = s })).Lift()).SwitchC(); bool defaultValue = o2.Count < 1; DiscreteCell <bool?> allSelected = objectsAndIsSelectedLocal.Map( oo => !oo.Any() ? defaultValue : (oo.All(o => o.IsSelected) ? true : (oo.All(o => !o.IsSelected) ? (bool?)false : null))); allSelectedCellLoop.Loop(allSelected.Cell); return(toggleAllSelectedStreamLocal, objectsAndIsSelectedLocal, selectAllStreamLocal, objectsLocal); }); // ReSharper disable once UnusedVariable IListener l = Transaction.Run(() => objectsAndIsSelected.Map(oo => oo.Count(o => o.IsSelected)).Updates.Listen(v => Console.WriteLine($"{v} selected"))); Console.WriteLine("Press any key"); Console.ReadKey(); Stopwatch sw = new Stopwatch(); sw.Start(); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); Thread.Sleep(500); SendMore(objects, selectAllStream); objects.Cell.Sample()[2].IsSelectedStreamSink.Send(true); Transaction.RunVoid(() => { objects.Cell.Sample()[3].IsSelectedStreamSink.Send(true); objects.Cell.Sample()[4].IsSelectedStreamSink.Send(true); }); Transaction.RunVoid(() => { objects.Send(Enumerable.Range(0, 2500).Select(n => new TestObject(n, selectAllStream)).ToArray()); toggleAllSelectedStream.Send(Unit.Value); }); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); toggleAllSelectedStream.Send(Unit.Value); objects.Send(new TestObject[0]); sw.Stop(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine($"Elapsed: {sw.ElapsedMilliseconds}ms"); Console.WriteLine(); Console.WriteLine("Press any key"); Console.ReadKey(); }
public Test(int initialValue) { this.Value = DiscreteCell.CreateSink(initialValue); }