public MainWindow()
        {
            this.InitializeComponent();

            Transaction.RunVoid(() =>
            {
                STextBox word           = new STextBox(string.Empty);
                CellLoop <bool> enabled = new CellLoop <bool>();
                SButton button          = new SButton(enabled)
                {
                    Content = "look up"
                };
                Stream <string> sWord = button.SClicked.Snapshot(word.Text);
                IsBusy <string, IMaybe <string> > ib = new IsBusy <string, IMaybe <string> >(Lookup, sWord);
                Stream <string> sDefinition          = ib.SOut.Map(o => o.Match(v => v, () => "ERROR!"));
                Cell <string> definition             = sDefinition.Hold(string.Empty);
                Cell <string> output = definition.Lift(ib.Busy, (def, bsy) => bsy ? "Looking up..." : def);
                enabled.Loop(ib.Busy.Map(b => !b));
                STextBox outputArea = new STextBox(Operational.Value(output), string.Empty, enabled)
                {
                    TextWrapping = TextWrapping.Wrap, AcceptsReturn = true
                };
                this.TextBoxPlaceholder.Child = word;
                this.ButtonPlaceholder.Child  = button;
                this.OutputPlaceholder.Child  = outputArea;
            });
        }
Exemple #2
0
            public Renderer(CompositionTargetSecondsTimerSystem timerSystem, Cell <DrawableDelegate> drawable, Size size, Animate animation, Cell <bool> isStarted)
            {
                this.drawable = drawable;
                this.size     = size;

                Transaction.Run(() => Operational.Value(isStarted).Filter(s => s).ListenOnce(_ => timerSystem.AddAnimation(animation)));
            }
Exemple #3
0
        public async Task TestListenOnceAsync()
        {
            BehaviorSink <int> b = Behavior.CreateSink(9);
            int result           = await Transaction.Run(() => Operational.Value(b).ListenOnceAsync());

            b.Send(2);
            b.Send(7);
            Assert.AreEqual(9, result);
        }
Exemple #4
0
        public async Task TestListenOnceAsync()
        {
            CellSink <int> c      = Cell.CreateSink(9);
            int            result = await Transaction.Run(() => Operational.Value(c).ListenOnceAsync());

            c.Send(2);
            c.Send(7);
            Assert.AreEqual(9, result);
        }
Exemple #5
0
        public void TestValue()
        {
            BehaviorSink <int> b    = Behavior.CreateSink(9);
            List <int>         @out = new List <int>();
            IListener          l    = Transaction.Run(() => Operational.Value(b).Listen(@out.Add));

            b.Send(2);
            b.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Exemple #6
0
        public void TestValueThenMap()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
        }
Exemple #7
0
        public void TestListenOnce()
        {
            CellSink <int> c    = Cell.CreateSink(9);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add));

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
Exemple #8
0
        public void TestValueThenMap()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).Map(x => x + 100).Listen(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 109, 102, 107 }, @out);
        }
Exemple #9
0
        public void TestListenOnce()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).ListenOnce(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9 }, @out);
        }
        public void Test_Value_TestCase2()
        {
            Tuple <Stream <char>, Dictionary <int, Action> > st = MkStream(new Dictionary <int, char> {
                { 0, 'b' }, { 1, 'c' }, { 3, 'd' }
            });
            Stream <char>            s  = st.Item1;
            Dictionary <int, Action> sf = st.Item2;
            Cell <char> c    = s.Hold('a');
            List <char> @out = RunSimulation <char>(h => Transaction.Run(() => Operational.Value(c).Listen(h)), new[] { sf });

            CollectionAssert.AreEqual(new[] { 'b', 'c', 'd' }, @out);
        }
Exemple #11
0
        public void TestValue()
        {
            CellSink <int> b    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(b).Listen(@out.Add)))
            {
                b.Send(2);
                b.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9, 2, 7 }, @out);
        }
Exemple #12
0
        public void TestValueThenFilter()
        {
            CellSink <int> c    = new CellSink <int>(9);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c).Filter(x => x % 2 != 0).Listen(@out.Add)))
            {
                c.Send(2);
                c.Send(7);
            }
            CollectionAssert.AreEqual(new[] { 9, 7 }, @out);
        }
Exemple #13
0
        public void TestValueThenLateListen()
        {
            CellSink <int> b     = new CellSink <int>(9);
            List <int>     @out  = new List <int>();
            Stream <int>   value = Operational.Value(b);

            b.Send(8);
            using (value.Listen(@out.Add))
            {
                b.Send(2);
            }
            CollectionAssert.AreEqual(new[] { 2 }, @out);
        }
Exemple #14
0
        public void TestValueThenMerge()
        {
            CellSink <int> c1   = new CellSink <int>(9);
            CellSink <int> c2   = new CellSink <int>(2);
            List <int>     @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(c1).Merge(Operational.Value(c2), (x, y) => x + y).Listen(@out.Add)))
            {
                c1.Send(1);
                c2.Send(4);
            }
            CollectionAssert.AreEqual(new[] { 11, 1, 4 }, @out);
        }
Exemple #15
0
        public void TestValueThenLateListen()
        {
            CellSink <int> c     = Cell.CreateSink(9);
            List <int>     @out  = new List <int>();
            Stream <int>   value = Operational.Value(c);

            c.Send(8);
            IListener l = value.Listen(@out.Add);

            c.Send(2);
            c.Send(7);
            l.Unlisten();
            CollectionAssert.AreEqual(new[] { 2, 7 }, @out);
        }
Exemple #16
0
        public void FunctionalBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Behavior.Loop <int>().WithoutCaptures(l => Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior());

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Exemple #17
0
        public void TestValueThenMerge()
        {
            CellSink <int> c1   = Cell.CreateSink(9);
            CellSink <int> c2   = Cell.CreateSink(2);
            List <int>     @out = new List <int>();
            IListener      l    = Transaction.Run(() => Operational.Value(c1).Merge(Operational.Value(c2), (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);
        }
Exemple #18
0
 public Frp(Action <string> addMessage)
 {
     this.listener = Transaction.Run(() =>
     {
         Cell <IMaybe <DragInfo> > dragInfo =
             this.sMouseDown.Map(me => Maybe.Just(new DragInfo(me, Canvas.GetLeft(me.Element.Polygon).ZeroIfNaN(), Canvas.GetTop(me.Element.Polygon).ZeroIfNaN())))
             .OrElse(this.sMouseUp.Map(_ => Maybe.Nothing <DragInfo>())).Hold(Maybe.Nothing <DragInfo>());
         Stream <MouseEvt> mouseMoveWhileDragging = dragInfo.Map(md => md.Match(d => this.sMouseMove, Stream.Never <MouseEvt>)).SwitchS();
         IListener listener1 = Operational.Value(dragInfo).FilterMaybe().Listen(d => addMessage("FRP dragging " + d.Me.Element.Name));
         IListener listener2 = mouseMoveWhileDragging.Snapshot(dragInfo, (me, md) => md.Match(
                                                                   d => Maybe.Just(new Reposition(d, me)),
                                                                   Maybe.Nothing <Reposition>)).FilterMaybe().Listen(p =>
         {
             Canvas.SetLeft(p.Polygon, p.Left);
             Canvas.SetTop(p.Polygon, p.Top);
         });
         return(new ImmutableCompositeListener(new[] { listener1, listener2 }));
     });
 }
Exemple #19
0
        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 = Operational.Updates(c).Listen(streamOutput.Add);

            List <IReadOnlyList <int> > cellOutput = new List <IReadOnlyList <int> >();
            IListener l2 = Transaction.Run(() => Operational.Value(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]);
        }
Exemple #20
0
        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();
        }
Exemple #21
0
        public void FunctionalBehaviorLoopWithCaptures()
        {
            BehaviorSink <int> s = Behavior.CreateSink(0);

            (Behavior <int> result, Behavior <int> s2) = Behavior.Loop <int>()
                                                         .WithCaptures(l => (Behavior: Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior(), Captures: s.Map(v => 2 * v)));

            List <int> @out = new List <int>();
            List <int> out2 = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
                using (Transaction.Run(() => Operational.Value(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);
        }
Exemple #22
0
 public Frp(Action <string> addMessage)
 {
     this.listener = Transaction.Run(() =>
     {
         Cell <IMaybe <DragInfo> > dragInfo =
             this.sMouseDown.Map(me => Maybe.Just(new DragInfo(me, Canvas.GetLeft(me.Element.Polygon).ZeroIfNaN(), Canvas.GetTop(me.Element.Polygon).ZeroIfNaN())))
             .OrElse(this.sMouseUp.Map(_ => Maybe.Nothing <DragInfo>())).Hold(Maybe.Nothing <DragInfo>());
         Cell <bool> axisLock = this.sShift.Hold(false);
         Cell <IMaybe <Tuple <MouseEvt, bool> > > mouseMoveAndAxisLock = dragInfo.Map(md => md.Match(
                                                                                          d => this.sMouseMove.Hold(d.Me).Lift(axisLock, Tuple.Create).Map(Maybe.Just),
                                                                                          () => Cell.Constant(Maybe.Nothing <Tuple <MouseEvt, bool> >()))).SwitchC();
         IListener listener1 = Operational.Value(dragInfo).FilterMaybe().Listen(d => addMessage("FRP dragging " + d.Me.Element.Name));
         IListener listener2 = Operational.Value(mouseMoveAndAxisLock).FilterMaybe().Snapshot(dragInfo, (ma, md) => md.Match(
                                                                                                  d => Maybe.Just(new Reposition(d, ma.Item1, ma.Item2)),
                                                                                                  Maybe.Nothing <Reposition>)).FilterMaybe().Listen(p =>
         {
             Canvas.SetLeft(p.Polygon, p.Left);
             Canvas.SetTop(p.Polygon, p.Top);
         });
         return(new ImmutableCompositeListener(new[] { listener1, listener2 }));
     });
 }
Exemple #23
0
        public void ImperativeBehaviorLoop()
        {
            BehaviorSink <int> s      = Behavior.CreateSink(0);
            Behavior <int>     result = Transaction.Run(
                () =>
            {
                BehaviorLoop <int> l       = new BehaviorLoop <int>();
                Behavior <int> resultLocal = Operational.Updates(s).Snapshot(l, (n, o) => n + o).Hold(0).AsBehavior();
                l.Loop(resultLocal);
                return(resultLocal);
            });

            List <int> @out = new List <int>();

            using (Transaction.Run(() => Operational.Value(result).Listen(@out.Add)))
            {
                s.Send(1);
                s.Send(2);
                s.Send(3);
            }

            CollectionAssert.AreEqual(new[] { 0, 1, 3, 6 }, @out);
        }
Exemple #24
0
 public static Stream <T> Changes <T>(this Cell <T> cell)
 {
     return(Operational.Value(cell).Snapshot(cell, (n, o) => EqualityComparer <T> .Default.Equals(o, n) ? Maybe.Nothing <T>() : Maybe.Just(n)).FilterMaybe());
 }