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 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 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 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 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 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 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 static void Main3(string[] args) { DiscreteCellSink <bool> c = new DiscreteCellSink <bool>(false); Console.WriteLine("Press any key"); Console.ReadKey(); ((Action)(() => { List <DiscreteCell <bool> > cc = new List <DiscreteCell <bool> >(); for (int i = 0; i < 5000; i++) { cc.Add(c.Map(v => !v)); } Console.WriteLine("Press any key"); Console.ReadKey(); }))(); Console.WriteLine("Press any key"); Console.ReadKey(); }
public static void Main2(string[] args) { Console.WriteLine("Press any key"); Console.ReadKey(); //DiscreteCellSink<IReadOnlyList<SmallTestObject>> s = ((Func<DiscreteCellSink<IReadOnlyList<SmallTestObject>>>)(() => // new DiscreteCellSink<IReadOnlyList<SmallTestObject>>(new SmallTestObject[0])))(); DiscreteCellSink <IReadOnlyList <SmallTestObject> > s = ((Func <DiscreteCellSink <IReadOnlyList <SmallTestObject> > >)(() => new DiscreteCellSink <IReadOnlyList <SmallTestObject> >(Enumerable.Range(0, 500).Select(_ => new SmallTestObject()).ToArray())))(); DiscreteCell <IReadOnlyList <bool> > s2 = s.Map(oo => oo.Select(o => o.S).Lift()).SwitchC(); ((Action)(() => { for (int i = 0; i < 5; i++) { s.Send(Enumerable.Range(0, 500).Select(_ => new SmallTestObject()).ToArray()); } }))(); s.Send(new SmallTestObject[0]); Console.WriteLine("Press any key"); Console.ReadKey(); ((Action)(() => { for (int i = 0; i < 5; i++) { s.Send(Enumerable.Range(0, 500).Select(_ => new SmallTestObject()).ToArray()); } }))(); s.Send(new SmallTestObject[0]); Console.WriteLine("Press any key"); Console.ReadKey(); }
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(); }