public void TestLoopCell() { CellSink <int> ca = Cell.CreateSink(22); (CellLoop <int> cb, Cell <int> cb2, Cell <int> cc) = Transaction.Run(() => { CellLoop <int> cbLocal = Cell.CreateLoop <int>(); Cell <int> ccLocal = ca.Map(x => x % 10).Lift(cbLocal, (x, y) => x * y); Cell <int> cbOut = ca.Map(x => x / 10); cbLocal.Loop(cbOut); return(cbLocal, cbOut, ccLocal); }); 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() { CellSink <int> b1 = new CellSink <int>(1); Cell <int> b3 = b1.Map(x => x * 3); Cell <int> b5 = b1.Map(x => x * 5); Cell <string> b = b3.Lift(b5, (x, y) => x + " " + y); List <string> @out = new List <string>(); using (b.Listen(@out.Add)) { b1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestLiftGlitch() { CellSink <int> c1 = new CellSink <int>(1); Cell <int> c3 = c1.Map(x => x * 3); Cell <int> c5 = c1.Map(x => x * 5); Cell <string> c = c3.Lift(c5, (x, y) => x + " " + y); List <string> @out = new List <string>(); using (c.Listen(@out.Add)) { c1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestSwitchCSimultaneous() { Sc2 sc1 = new Sc2(0); CellSink <Sc2> csc = new CellSink <Sc2>(sc1); Cell <int> co = csc.Map <Cell <int> >(b => b.C).SwitchC(); List <int> @out = new List <int>(); using (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); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void TestSwitchEarlySSimultaneous() { Ss2 ss1 = new Ss2(); CellSink <Ss2> css = Cell.CreateSink(ss1); Stream <int> so = css.Map <Stream <int> >(b => b.S).SwitchEarlyS(); List <int> @out = new List <int>(); IListener l = so.Listen(@out.Add); Ss2 ss2 = new Ss2(); Ss2 ss3 = new Ss2(); Ss2 ss4 = new Ss2(); ss1.S.Send(0); ss1.S.Send(1); ss1.S.Send(2); css.Send(ss2); ss1.S.Send(7); ss2.S.Send(3); ss2.S.Send(4); ss3.S.Send(2); css.Send(ss3); ss3.S.Send(5); ss3.S.Send(6); ss3.S.Send(7); Transaction.RunVoid(() => { ss4.S.Send(8); css.Send(ss4); ss3.S.Send(2); }); ss4.S.Send(9); l.Unlisten(); CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void TestStreamSendInCellMapThrowsException() { InvalidOperationException actual = null; CellSink <int> c = Cell.CreateSink(5); StreamSink <int> s2 = Stream.CreateSink <int>(); try { using (c.Map( v => { s2.Send(v); return(Unit.Value); }) .Listen(_ => { })) { } } catch (InvalidOperationException e) { actual = e; } Assert.IsNotNull(actual); Assert.AreEqual("Send may not be called inside a Sodium callback.", actual.Message); }
public void TestMap() { CellSink <int> c = new CellSink <int>(6); List <string> @out = new List <string>(); using (c.Map(x => x.ToString()).Listen(@out.Add)) { c.Send(8); } CollectionAssert.AreEqual(new[] { "6", "8" }, @out); }
public async Task TestListenAsync() { CellSink <int> a = Cell.CreateSink(1); Cell <int> a1 = a.Map(x => x + 1); Cell <int> a2 = a.Map(x => x * 2); (List <int> results, CellLoop <int> called, IListener l) = Transaction.Run(() => { Cell <int> result = a1.Lift(a2, (x, y) => x + y); Stream <Unit> incrementStream = result.Values().MapTo(Unit.Value); StreamSink <Unit> decrementStream = Stream.CreateSink <Unit>(); CellLoop <int> calledLoop = Cell.CreateLoop <int>(); calledLoop.Loop(incrementStream.MapTo(1).Merge(decrementStream.MapTo(-1), (x, y) => x + y).Snapshot(calledLoop, (u, c) => c + u).Hold(0)); List <int> r = new List <int>(); IListener lLocal = result.Listen(v => { Task.Run(async() => { await Task.Delay(900); r.Add(v); decrementStream.Send(Unit.Value); }); }); return(r, calledLoop, lLocal); }); // ReSharper disable once UnusedVariable 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(); l.Unlisten(); }
public void TestMapLateListen() { CellSink <int> b = new CellSink <int>(6); List <string> @out = new List <string>(); Cell <string> bm = b.Map(x => x.ToString()); b.Send(2); using (bm.Listen(@out.Add)) { b.Send(8); } CollectionAssert.AreEqual(new[] { "2", "8" }, @out); }
public void TestLiftSimultaneousUpdates() { List <int> @out = new List <int>(); CellSink <int> cellSink = Cell.CreateSink(1); Cell <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 TestRunConstruct2() { var(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 <TestObject2> o2 = Enumerable.Range(0, 10000).Select(n => new TestObject2(n, n < 1500, selectAllStreamLocal)).ToArray(); CellSink <IReadOnlyList <TestObject2> > objectsLocal = Cell.CreateSink(o2); var objectsAndIsSelectedLocal = objectsLocal.Map(oo => oo.Select(o => o.IsSelected.Map(s => new { Object = o, IsSelected = s })).Lift()).SwitchC(); bool defaultValue = o2.Count < 1; Cell <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); return(objectsAndIsSelectedLocal, selectAllStreamLocal, objectsLocal); }); List <int> @out = new List <int>(); using (Transaction.Run( () => objectsAndIsSelected.Map(oo => oo.Count(o => o.IsSelected)) .Values.Listen(@out.Add))) { Transaction.Run(() => { objects.Send( Enumerable.Range(0, 20000) .Select(n => new TestObject2(n, n < 500, selectAllStream)) .ToArray()); return(Unit.Value); }); } CollectionAssert.AreEqual(new[] { 1500, 500 }, @out); }
public void TestMapWithSwitchC() { 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) }; CellSink <IReadOnlyList <Test> > v = Cell.CreateSink(list1); Cell <IReadOnlyList <int> > c = v.Map(oo => oo.Select(o => o.Value).Lift()).Map(o => o).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]); }
private static void Main(string[] args) { CellSink <string> csInput = new CellSink <string>(string.Empty); Cell <string> cInput = csInput.Map(x => x); Cell <List <string> > tokens = cInput.Map(Tokenize); Cell <string> cOutput = cInput.Map(GetResponse); Cell <bool> exit = cInput.Map(v => cGoodbye.Sample().Contains(v)); Cell <bool> randomWord = cInput.Map(i => i.Contains("random")); while (!exit.Sample()) { W("You: "); csInput.Send(Console.ReadLine()); W("SodiumBot: "); Wl(randomWord.Sample() ? GetRandomIndexOf(tokens.Sample()) : cOutput.Sample()); Wl(tokens.Sample().Count > 3 ? "word count: " + tokens.Sample().Count : string.Empty); } Console.ReadKey(); }
public static void Main3(string[] args) { CellSink <bool> c = new CellSink <bool>(false); Console.WriteLine("Press any key"); Console.ReadKey(); ((Action)(() => { List <Cell <bool> > cc = new List <Cell <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(); //CellSink<IReadOnlyList<SmallTestObject>> s = ((Func<CellSink<IReadOnlyList<SmallTestObject>>>)(() => // new CellSink<IReadOnlyList<SmallTestObject>>(new SmallTestObject[0])))(); CellSink <IReadOnlyList <SmallTestObject> > s = ((Func <CellSink <IReadOnlyList <SmallTestObject> > >)(() => new CellSink <IReadOnlyList <SmallTestObject> >(Enumerable.Range(0, 500).Select(_ => new SmallTestObject()).ToArray())))(); Cell <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 void FunctionalCellLoopWithCaptures() { CellSink <int> s = Cell.CreateSink(0); (Cell <int> result, Cell <int> s2) = Cell.Loop <int>() .WithCaptures(l => (Cell: s.Updates().Snapshot(l, (n, o) => n + o).Hold(0), Captures: s.Map(v => 2 * v))); List <int> @out = new List <int>(); List <int> out2 = new List <int>(); using (Transaction.Run(() => result.Listen(@out.Add))) using (Transaction.Run(() => s2.Listen(out2.Add))) { s.Send(1); s.Send(2); s.Send(3); } CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out); CollectionAssert.AreEqual(new[] { 0, 2, 4, 6 }, out2); }
public void TestLiftGlitch() { CellSink<int> b1 = new CellSink<int>(1); Cell<int> b3 = b1.Map(x => x * 3); Cell<int> b5 = b1.Map(x => x * 5); Cell<string> b = b3.Lift(b5, (x, y) => x + " " + y); List<string> @out = new List<string>(); using (b.Listen(@out.Add)) { b1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestMap() { CellSink<int> b = new CellSink<int>(6); List<string> @out = new List<string>(); using (b.Map(x => x.ToString()).Listen(@out.Add)) { b.Send(8); } CollectionAssert.AreEqual(new[] { "6", "8" }, @out); }
public void TestMapLateListen() { CellSink<int> c = new CellSink<int>(6); List<string> @out = new List<string>(); Cell<string> cm = c.Map(x => x.ToString()); c.Send(2); using (cm.Listen(@out.Add)) { c.Send(8); } CollectionAssert.AreEqual(new[] { "2", "8" }, @out); }
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(); CellSink <IReadOnlyList <TestObject> > objectsLocal = Cell.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; Cell <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); 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.Sample()[2].IsSelectedStreamSink.Send(true); Transaction.RunVoid(() => { objects.Sample()[3].IsSelectedStreamSink.Send(true); objects.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 void TestLiftGlitch() { CellSink<int> c1 = new CellSink<int>(1); Cell<int> c3 = c1.Map(x => x * 3); Cell<int> c5 = c1.Map(x => x * 5); Cell<string> c = c3.Lift(c5, (x, y) => x + " " + y); List<string> @out = new List<string>(); using (c.Listen(@out.Add)) { c1.Send(2); } CollectionAssert.AreEqual(new[] { "3 5", "6 10" }, @out); }
public void TestSwitchCSimultaneous() { Sc2 sc1 = new Sc2(0); CellSink<Sc2> csc = new CellSink<Sc2>(sc1); Cell<int> co = csc.Map<Cell<int>>(b => b.C).SwitchC(); List<int> @out = new List<int>(); using (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); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }
public void TestSwitchSSimultaneous() { Ss2 ss1 = new Ss2(); CellSink<Ss2> css = new CellSink<Ss2>(ss1); Stream<int> so = css.Map<Stream<int>>(b => b.S).SwitchS(); List<int> @out = new List<int>(); using (so.Listen(@out.Add)) { Ss2 ss2 = new Ss2(); Ss2 ss3 = new Ss2(); Ss2 ss4 = new Ss2(); ss1.S.Send(0); ss1.S.Send(1); ss1.S.Send(2); css.Send(ss2); ss1.S.Send(7); ss2.S.Send(3); ss2.S.Send(4); ss3.S.Send(2); css.Send(ss3); ss3.S.Send(5); ss3.S.Send(6); ss3.S.Send(7); Transaction.RunVoid(() => { ss3.S.Send(8); css.Send(ss4); ss4.S.Send(2); }); ss4.S.Send(9); } CollectionAssert.AreEqual(new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, @out); }